- "matlab suggest me to put Color as dependent property" are you sure you didn't misunderstand the text?
- In a handle class you can put the test in a Property Set Method. See Comparison of Handle and Value Classes and Property Set Method
How to put condition (based on a property) in a property accessor
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I have a class property, which value is conditionned by another property of the class.
Let say I have a "Car" class with "brand" and "color" as property.
if the brand is Ferrari then the color can only be red or yellow, but is the brand is Mercedes, then color can only be Black or Silver.
The way I write it, matlab suggest me to put Color as dependent property. However color has to remain a user input, which seems not possible if it is set as dependent property...
Thanks in advance for your help.
1 comentario
per isakson
el 25 de Oct. de 2017
Respuestas (1)
Yair Altman
el 25 de Oct. de 2017
Editada: Yair Altman
el 26 de Oct. de 2017
I suggest that you create a setter method for color that will check the relevant condition, for example:
function set.color(obj,value)
if strcmpi(obj.brand,'Ferrari')
if ~any(strcmpi(value,{'red','yellow'}))
error('bad_value', 'The color of Ferrari can only be red/yellow!')
end
elseif strcmpi(obj.brand,'Mercedes')
if ~any(strcmpi(value,{'black','silver'}))
error('bad_value', 'The color of Mercedes can only be black/silver!')
end
end
end
If you allow the user to modify the brand after the color was set, then you might want to add a corresponding setter method for the brand property as well.
3 comentarios
Adam
el 26 de Oct. de 2017
Editada: Adam
el 26 de Oct. de 2017
That error should link you to an explanation for that, but yes, you will need it to be a dependent property. Dependent properties are not only for getting purposes even though that is their intuitive use. You can use a dependent property with a set function also, but with a few caveats. In this case you need to use one because only a dependent property is allowed to have a set function that accesses other class properties. Or rather, that is recommended. It is a warning rather than an error, if I remember correctly. You can ignore it and the cases that it points to may never affect you, but ignoring warnings is not usually a good idea unless you are very well aware of what the warning is telling you and that it does not apply. Here it would just be bad practice to ignore it.
Mainly you need to use a second, private, variable to store the actual value. Usually I would create a colour_ variable for this, then in the set function like shown in the answer you would add
obj.colour_ = value;
at the end and you then also need to create a getter for your colour property as every dependent property must have a getter (though a setter is not compulsory and in normal usage is not desired, but in this case it is).
i.e.
function color = get.color( obj )
color = obj.colour_;
end
So in your properties you would have:
properties( Dependent, Access = public )
color;
end
properties( Access = private )
colour_;
end
Obviously you can feel free to spell your private variable in the non-English spelling too :)
Ver también
Categorías
Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!