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

采纳的回答

Matt J
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 CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by