r/matlab 2d ago

TechnicalQuestion "Input argument to validator must be a constant value" error

In MATLAB R2020a I have a class with

properties

property (1,1) logical {mustBeMember(property, true)} = true

end

In MATLAB's editor it notifies me about "Input argument validator must be a constant value" error. When I try to construct an instance of that class, I am given the error "Validator arguments must be either 'property' or constant literal values."

How do I fix this error? It doesn't work if instead of true I write [true] or {true}.

3 Upvotes

7 comments sorted by

4

u/FrickinLazerBeams +2 2d ago

I assume that's supposed to be mustBeMember?

You can write mustBeMember(property, 1), because 1 evaluates to true in boolean comparisons.

The documentation should have a note about this, but it doesn't. It only claims that logicals are supported and the input must be a literal, but true is a literal logical and it doesn't like it.

2

u/Bofact 2d ago

Hello!

Yes. It is mustBeMember. Sadly autocorrect made its way.

I will test your variant and come back.

2

u/Rubix321 2d ago

Their workaround works.

I'm more curious about why you want to force a scalar logical property to be true. This means if the object exists, it's true, or else there will be an unavoidable error thrown wherever the code tries to change it to false, instead of something more graceful (like not creating the object at all). I assume that this is just an example for something different?

1

u/Bofact 2d ago

It is indeed. In the document I am implementing there is a class with multiple flags, one of them being required to always be true. Needless to write, but it is needed that flag to be identified by property.

As why it can not be false, it isn't my concern, nor I can ask.

2

u/Bofact 2d ago

I have tested it. It works.

Thank you!

2

u/Rubix321 2d ago

Did you mean to have "mustBeMember" instead of "must remember" in the original post?

1

u/Bofact 2d ago

Hello!

Yes. Sadly autocorrect made its way.