Function Argument Validation: Comparing property size to a variable

4 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Matt J
Matt J am 5 Sep. 2023
Bearbeitet: Matt J am 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 Kommentar
Bradley Treeby
Bradley Treeby am 5 Sep. 2023
Bearbeitet: Bradley Treeby am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Shubham
Shubham am 29 Mai 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 Kommentar
Bradley Treeby
Bradley Treeby am 5 Sep. 2023
Bearbeitet: Bradley Treeby am 5 Sep. 2023
Thanks! But this doesn't validate the size of obj.matrix if the values are changed outside the constructor right?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by