MATLAB Object-Oriented Programming

(Joyce) #1

Assignment to the Value property is equivalent to indexed assignment of an array. If the
assigned value is out of the range of values that uint8 can represent, MATLAB sets the
value to the closest value that it can represent using uint8.


a = uint8.empty;
a(1) = -10


a =


uint8


0


To avoid the potential for integer overflow, use a combination of validation functions that
restrict the value to the intended range instead of an integer class.


classdef IntProperty
properties
Value(1,1) {mustBeInteger, mustBeNonnegative,...
mustBeLessThan(Value,256)}
end
end


Because there is no conversion of the assigned value by the uint8 class, the validators
catch out of range values and throw an appropriate error.


a = IntProperty;
a.Value = -10;


Error setting property 'Value' of class 'IntProperty':
Value must be nonnegative.


Default Values Per Size and Class


Any default property value that you assign in the class definition must conform to the
specified validation.


Implicit Default Values


MATLAB defines a default value implicitly if you do not specify a default value in the class
definition. This table shows how size and class determine the implicit default value of
MATLAB classes.


Property Class and Size Validation
Free download pdf