How do I use a class property validation function with a dynamic argument?

조회 수: 4 (최근 30일)
In Matlab OOP, I wanna use a function like mustBeEqualSize(a,b) from the Examples on function property validation to validate a property:
classdef foo
properties
a
b {mustBeEqualSize(a,b)}
end
end
However, this gives me an error.

채택된 답변

Jan Siegmund
Jan Siegmund 2020년 11월 14일
The problem is, that MATLAB classes only support constant arguments on property validation.
However function arguments can be validated perfectly fine with dynamic arguments.
So a workaround is to validate the arguments of the constructor:
classdef Foo
properties
a
b
end
methods
function obj = Foo(a,b)
arguments
a
b {mustBeEqualSize(a,b)}
end
obj.a = a;
obj.b = b;
end
end
end
  댓글 수: 4
Benjamin Penazek
Benjamin Penazek 2024년 10월 1일
One thing that does half the job is overloading the ismember function for your class. Something like the following. I say half the job, because it "works" in the way you want, but it does not offer autocomplete (no popup with 'RMS Level', 'Gain', etc. will appear when you try to call obj.addPrimaryResult()).
methods
function boolOut = ismember(val, obj)
boolOut = ismember(val, obj.possibleResults);
end
end
Steven Lord
Steven Lord 2024년 10월 1일
Try this:
classdef ContinuousSweep < handle
properties (Constant)
possibleResults = {'RMS Level','Gain','Phase','THD Ratio','THD Level','THD+N Ratio','THD+N Level','Impulse Response'}
end
methods
function obj = addPrimaryResult(obj,result)
%addPrimaryResult Adds a result of the specified type if it is allowed for this measurement type.
arguments (Input)
obj
result string {mustBePossibleResult(result)}
end
disp(result)
end %addPrimaryResult
end
end
function mustBePossibleResult(result)
PR = ContinuousSweep.possibleResults;
assert(ismember(result, PR), ...
result + " must be one of: " + newline + strjoin(PR, ", "))
end
You can reuse mustBePossibleResult in multiple methods, as a locally defined validation function. If the property is not Constant (and so you need to retrieve the value from the instance rather than from the class) you may need to include obj in the argument list of mustBePossibleResult but since that's a previous input argument that's okay. Then you could get obj.possibleResults inside mustBePossibleResult.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by