Function Argument Validation: Comparing property size to a variable

8 views (last 30 days)
I would like to ensure that a property of a class is of a certain size, defined by another property of the class which is set in the constructor. I can implement this by using a private property and set and get methods as shown in the code example below:
classdef TestClassI < handle
properties(SetAccess=immutable)
matrixSize single
end
properties(Dependent)
matrix single {mustBeNumeric}
end
properties(Access=private, Hidden=true)
matrix_
end
methods
function obj = TestClassI(matrixSize)
obj.matrixSize = matrixSize;
end
function matrix = get.matrix(obj)
matrix = obj.matrix_;
end
function set.matrix(obj, val)
validateattributes(val, {'numeric'}, {'size', obj.matrixSize});
obj.matrix_ = val;
end
end
end
However, this is pretty cumbersome. What I'd prefer to do is something like:
classdef TestClassII < handle
properties(SetAccess=immutable)
matrixSize single
end
properties
matrix(10,1) single {mustBeNumeric}
end
methods
function obj = TestClassII(matrixSize)
obj.matrixSize = matrixSize;
end
end
end
But, where (10,1) is not hard-coded but given by matrixSize. Is there a way to do this?

Accepted Answer

Matt J
Matt J on 5 Sep 2023
Edited: Matt J on 5 Sep 2023
No, you can't do that, because the object has to be able to instantiate properties before the constructor is called.Therefore, property validation steps won't be able to use data that is only brought into existence later by the constructor.
My recommendation would be to overload subsasgn(). There, you can define any object-dependent validation steps that you want.
Or, if you really need the validation to be done even internally within the classdef, you could use matlab.mixin.indexing.RedefinesDot, though in my experience, that route can be more delicate.
  1 Comment
Bradley Treeby
Bradley Treeby on 5 Sep 2023
Edited: Bradley Treeby on 5 Sep 2023
> the object has to be able to instantiate properties before the constructor is called
Got it, that makes sense.
Note to my future self, it's also possible to do the same thing as I originally posted but without an internal variable. The MATLAB code analyzer warns against a set method accessing another property, but I think it's safe in this scenario, as matrixSize is declared as immutable.
classdef TestClassIII < handle
properties(SetAccess=immutable)
matrixSize single
end
properties
matrix single {mustBeNumeric}
end
methods
function obj = TestClassIII(matrixSize)
obj.matrixSize = matrixSize;
end
function set.matrix(obj, val)
validateattributes(val, {'numeric'}, {'size', obj.matrixSize});
obj.matrix = val;
end
end
end

Sign in to comment.

More Answers (1)

Shubham
Shubham on 29 May 2023
Hi Bradley,
Yes, there is a way to achieve this without the use of set and get methods. One possible solution is to use the validateattributes function in the constructor to check that the matrix property has the correct size. Here's an example implementation:
classdef TestClassII < handle
properties(SetAccess=immutable)
matrixSize single
end
properties
matrix single {mustBeNumeric}
end
methods
function obj = TestClassII(matrixSize)
obj.matrixSize = matrixSize;
defaultSize = [matrixSize, 1];
defaultMat = zeros(defaultSize, 'single');
validateattributes(defaultMat, {'numeric'}, {'size', defaultSize});
obj.matrix = defaultMat;
end
end
end
In this implementation, we initialize the matrix property using a default size of [matrixSize, 1] and fill it with zeros using the zeros function. Then we use validateattributes to check that the size of the default matrix is correct.
Note that validateattributes raises an error if the validation fails, so we only set obj.matrix if the validation succeeds.
  1 Comment
Bradley Treeby
Bradley Treeby on 5 Sep 2023
Edited: Bradley Treeby on 5 Sep 2023
Thanks! But this doesn't validate the size of obj.matrix if the values are changed outside the constructor right?

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!