How can I combine mustBeA and mustBeNumeric in argument validation functions
14 次查看(过去 30 天)
显示 更早的评论
Hallo All,
I am new to argument validation functions. I would to parse a 'double', 'ss' or 'tf' data type to my function as follows:
>> LSfunc(Controller)
The LSfunc is a example function to get my question accross. The function looks look this:
function Out = LSfunc(In)
% Check arguments
arguments
In (1,1) {mustBeA(In, "tf", "ss")
end
% Convert numeric to ss data-type
In = ss(In)
% Whenever the data type is ss print: "Great job!"
if isa(In, 'ss')
display('Great job!');
end
end
As you can see I do not have a problem with allowing the input In to be any numeric because I convert it inside the function.
So my question boils down to this:
How can I combine mustBeA(In, "tf", "ss") and mustBeNumeric(In)? (P.S. Yes, I could convert the input before it enters the function but I do not want this because my script gets quit long this way)
Thanks for the help!
With kind regards,
Luuk
0 个评论
采纳的回答
Matt J
2021-11-1
编辑:Matt J
2021-11-1
You could write your own custom validation function for such a purpose, e.g.,
% Custom validation function
function mustBeNumericOrListedType(a,varargin)
if ~isnumeric(a) && ~ismember(class(a),cellstr(varargin))
eid = 'Type:notValid';
msg = 'Argument must be numeric or one of the prescribed types';
throwAsCaller(MException(eid,msg))
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!