Azzera filtri
Azzera filtri

How can I combine mustBeA and mustBeNumeric in argument validation functions

14 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Matt J
Matt J il 1 Nov 2021
Modificato: Matt J il 1 Nov 2021
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

Più risposte (0)

Categorie

Scopri di più su Argument Definitions in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by