When attempting to restrict the set of values assignable to a property by using class validation, I find that the values being assigned are instead being converted instead of throwing an error. For example, in the following class, I would like MATLAB to throw an error when attempting to assign a negative value to Property1:
classdef Class1 properties Property1 uint32 {isNonNeg} = uint32(0) endendfunction isNonNeg(val) if ~(isinteger(val) && val >= 0) error("Property1 must be assigned a nonnegative integer value."); endend
However, the following code succeeds:
>> x = Class1();>> x.Property1 = -5x = Class1 with properties: Property1: 0
Best Answer