ARGCHK

Validates parameters passed to a function.
793 Downloads
Aktualisiert 2. Aug 2005

Keine Lizenz

ARGCHK validates parameters passed to a function.

[ErrorMsg, ErrorNo] = ARGCHK(ParmSpec,FuncName)
returns an error message if any input parameter to the function
FuncName does not meet the specification as set by ParmSpec.

ParmSpec = {x, data type, range, severity, name}

x = parameter to be checked
data type 'real', 'integer', 'scalar', 'string', 'vector', or 'matrix'
range = [a b]; a = lower limit; b = upper limit
severity = 'warning' or 'error'
name = string identifying parameter x e.g. 'X' or 'pCheck'

See ARGCHK help for more details.

Example,

function [a,b] = pol2car(x,y,str)
% This function takes two values x and y and converts them into
% Polar coordinates, a and b if str = 'polar' or into Cartesian
% coordinates if str = 'cartesian'.

error(nargchk(3, 3, nargin)); % check number of input arguments

p(1,:) = {x 'real' [0 1E6] 'warning' 'x'};
p(2,:) = {x 'scalar' [] 'warning' 'x'};
p(3,:) = {y 'real' [0 1E6] 'error' 'y'};
p(4,:) = {y 'scalar' [] 'error' 'y'};
p(5,:) = {str 'string' [{'polar'} {'cartesian'}] 'error' 'str'};

% p is the parameter specification

[ErrorMsg, ErrorNo] = argchk(p,'pol2car');
if isempty(char(ErrorMsg))
switch str
case 'polar'
a = sqrt(x^2 + y^2);
b = angle(x+j*y);
case 'cartesian'
a = x*cos(y);
b = x*sin(y);
end
else
a = [];
b = [];
for i = 1:length(ErrorMsg)
disp([num2str(ErrorNo(i)),' ', char(ErrorMsg(i))]);
end
end

The following errors are returned when the function is incorrectly called with:

>> [u,v] = pol2car(-2,[1:8],'artesian');
1 POL2CAR ERROR: Input parameter x must be between 0 and 1000000.
4 POL2CAR WARNING: Input parameter y must be scalar.
5 POL2CAR ERROR: Input parameter str must be an exact string match.

Zitieren als

J De Freitas (2024). ARGCHK (https://www.mathworks.com/matlabcentral/fileexchange/7949-argchk), MATLAB Central File Exchange. Abgerufen .

Kompatibilität der MATLAB-Version
Erstellt mit R14SP1
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS Linux
Kategorien
Mehr zu Functions finden Sie in Help Center und MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Veröffentlicht Versionshinweise
1.0.0.0

Update