| Contents | Index |
validateattributes(A,classes,attributes)
validateattributes(A,classes,attributes,argIndex)
validateattributes(A,classes,attributes,funcName)
validateattributes(A,classes,attributes,funcName,varName)
validateattributes(A,classes,attributes,funcName,varName,argIndex)
validateattributes(A,classes,attributes) validates that array A belongs to at least one of the specified classes (or its subclass) and has all of the specified attributes. If A does not meet the criteria, MATLAB throws an error and displays a formatted error message. Otherwise, validateattributes completes without displaying any output.
validateattributes(A,classes,attributes,argIndex) includes the position of the input in your function argument list as part of any generated error messages.
validateattributes(A,classes,attributes,funcName) includes the specified function name in generated error identifiers.
validateattributes(A,classes,attributes,funcName,varName) includes the specified variable name in generated error messages.
validateattributes(A,classes,attributes,funcName,varName,argIndex) includes the specified information in generated error messages or identifiers.
Check the size of an array.
classes = {'numeric'};
attributes = {'size',[4,6,2]};
A = rand(3,5,2);
validateattributes(A,classes,attributes)Because A is not of the expected size, validateattributes throws an error and displays this message:
Expected input to be of size 4x6x2 when it is actually size 3x5x2.
Check that the values in an array are 8-bit integers within a specific range.
classes = {'uint8','int8'};
attributes = {'>',0,'<',70};
A = randi(100,4,6,2,'uint8');
validateattributes(A,classes,attributes)If some of the values are outside the range, MATLAB displays this error message:
Expected input to be an array with all of the values < 70.
Check the attributes of a complex number.
a = complex(1,1);
validateattributes(a,{'numeric'},{'nonnegative'}); MATLAB displays this error message:
Expected input to be nonnegative.
Because complex numbers lack a well-defined ordering in the complex plane, validateattributes does not recognize them as positive or negative.
Check inputs to a custom function, and include information about the input name and position in generated errors.
function v = findVolume(shape,ht,wd,ln)
validateattributes(shape,{'char'},{'nonempty'},mfilename,'Shape',1)
validateattributes(ht,{'numeric'},{'nonempty'},mfilename,'Height',2)
validateattributes(wd,{'numeric'},{'nonempty'},mfilename,'Width',3)
validateattributes(ln,{'numeric'},{'nonempty'},mfilename,'Length',4)When you call the function without the shape input string,
vol = findVolume(10,7,4)
MATLAB displays
Error using findVolume Expected input number 1, Shape, to be one of these types: char Instead its type was double.
The function name is part of the error identifier, so
MException.last.identifier
returns
ans =
MATLAB:findVolume:invalidTypeCreate a custom function that checks input parameters with inputParser, and use validateattributes as the validating function for the addRequired and addOptional methods.
function a = findArea(shape,dim1,varargin)
p = inputParser;
charchk = {'char'};
numchk = {'numeric'};
nempty = {'nonempty'};
addRequired(p,'shape',@(x)validateattributes(x,charchk,nempty));
addRequired(p,'dim1',@(x)validateattributes(x,numchk,nempty));
addOptional(p,'dim2',1,@(x)validateattributes(x,numchk,nempty));
parse(p,shape,dim1,varargin{:});
switch shape
case 'circle'
a = pi * dim1.^2;
case 'rectangle'
a = dim1 .* p.Results.dim2;
endIf you call the function with a nonnumeric third input,
myarea = findArea('rectangle',3,'x')MATLAB displays
Error using findArea Argument 'dim2' failed validation with error: Expected input to be one of these types: numeric Instead its type was char.
inputParser | is* | isa | isnumeric | validatestring

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |