Validates parameters passed to a function.
Updated 2 Aug 2005

No License

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.


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);
a = [];
b = [];
for i = 1:length(ErrorMsg)
disp([num2str(ErrorNo(i)),' ', char(ErrorMsg(i))]);

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.

Cite As

J De Freitas (2024). ARGCHK (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14SP1
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Functions in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes