MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

% All properties occur inside a properties declaration.
% These properties have public access (the default)
properties (Logical)
UseIncrement = true
end


properties (PositiveInteger)
Increment = 1
WrapValue = 10
end


methods
% Validate the properties of the object
function set.Increment(obj,val)
if val >= 10
error("The increment value must be less than 10");
end
obj.Increment = val;
end
end


methods (Access = protected)
function validatePropertiesImpl(obj)
if obj.UseIncrement && obj.WrapValue > obj.Increment
error("Wrap value must be less than increment value");
end
end


% Validate the inputs to the object
function validateInputsImpl(~,x)
if ~isnumeric(x)
error("Input must be numeric");
end
end


function out = stepImpl(obj,in)
if obj.UseIncrement
out = in + obj.Increment;
else
out = in + 1;
end
end


Validate Property and Input Values
Free download pdf