How to implement a setter restriction?

When making a user-defined class myClass that inherits from matlab.mixin.SetGet, how can I restrict the Set for a property prop1? Say that I want to allow only true/false sets for a property.
classdef myClass < handle & matlab.mixin.SetGet
properties (Access = public)
prop1
end
methods
%% Setter restrictions
% This is what I would do if it weren't a subclass of matlab.mixin.SetGet {
function set.prop1(self,value)
% Prevent non-boolean input value sets.
if value == true
self.prop1 = true;
else
self.prop1 = false;
end
end
%}
end
end
I like the syntax of matlab.mixin.SetGet so I'd prefer to not code it this way.

5 件のコメント

Walter Roberson
Walter Roberson 2019 年 1 月 31 日
assert(isscalar(value) && islogical(value), 'Must be boolean scalar')
I would note, though, that it is very common for people to pass 0 for false or 1 for true, and that as far as MATLAB is concerned, anything non-zero non-nan is true.
Dominik Mattioli
Dominik Mattioli 2019 年 1 月 31 日
I'm sort of confused on where this would go? I should have clarified that I'm hoping for the answer to this question to be a function that replaces the function in my example.
Walter Roberson
Walter Roberson 2019 年 1 月 31 日
It would go right after
% Prevent non-boolean input value sets.
Are you hoping for a mechanism to add type restrictions on the calls themselves, such as (hypothetically) through the methods attributes list, so that MATLAB does the type filtering for you, without you having to put in a type check yourself ?
Dominik Mattioli
Dominik Mattioli 2019 年 1 月 31 日
Ah I see.
I am not necessarily looking fo that extensive of a restriction. Ideally, your suggested code would be enough. Then a set method call would look like this (ignoring that I forgot to write a constructor in the original question):
mc = myClass();
mc.set('prop1',false) % or: mc.set('prop1',0);
%%% Error produced by:
propVal = 99
mc.set('prop1',propVal)
Walter Roberson
Walter Roberson 2019 年 1 月 31 日
If you want to permit 0 and 1 as well as true and false, then I note that ismember() of 0 or 1 against true false works, and ismember() of true or false against 0 1 works, and that true == 1 and 0 == false work.

サインインしてコメントする。

 採用された回答

Steven Lord
Steven Lord 2019 年 2 月 1 日

1 投票

If you just want to accept 0, 1, true, and false try using the mustBeNumericOrLogical property validation function. See the "Constrain Property to Numeric or Logical Values" example on that documentation page to see how to use it.

2 件のコメント

Dominik Mattioli
Dominik Mattioli 2019 年 2 月 1 日
Walter's comments were helpful but this is exactly what I'm looking for.
Steven Lord
Steven Lord 2019 年 2 月 1 日
You may want to also include some of the other property validation functions to lock down not only the type of the property but also the value. mustBeGreaterThanOrEqual and mustBeLessThanOrEqual or mustBeMember are possibilities, or you could define your own validation function as described in the "Define Validation Functions" section on the page to which I linked in the first line of this comment.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by