list of possible attributes in validateattributes

18 views (last 30 days)
Rik
Rik on 20 Sep 2017
Edited: Cedric on 20 Sep 2017
I have a function that accepts an nx2 or nx3 array. I want to have a robust method of checking my inputs, which is why I use validateattributes. I have not been able to find how to supply a list of attributes where only 1 has to apply. I currently solve this with the code below, but I find this workaround to be very inelegant (and it is hard to generalize this solution). I have the same problem with another input that can be 1x2, 2x1, 1x3 or 3x1.
validateattributes(pt,{'numeric'},{'2d','nonempty'})
if ~any(size(pt,2)==[2 3])
%Trigger nicely formatted error (3D case is the expected default)
validateattributes(pt,{'numeric'},{'size',[NaN 3]})
end
Is there a solution I have missed, or is this the only way to do this?
(for context: my FEX submission)
  2 Comments
Brendan Hamm
Brendan Hamm on 20 Sep 2017
You may consider looking at the input parser which allows you to provide your own custom validation function. I'm not sure the solution would be more "elegant". Is there any specific reason you want to use validateattributes? It seems as you are calling in only when you expect it to fail, you may as well just throw your own error using either:
error(ID,msg)
or
MException(...)
Cedric
Cedric on 20 Sep 2017
Edited: Cedric on 20 Sep 2017
Here is a quick example using an input parser, in case:
function foo( varargin )
parser = inputParser ;
parser.StructExpand = true ;
parser.CaseSensitive = false ;
parser.addRequired( 'filename', @ischar ) ;
parser.addRequired( 'data', [], ...
@(x) isnumeric(x) || iscell(x) ) ;
parser.addOptional( 'version', 1, @isnumeric) ;
parser.addParameter( 'separator', '-', @ischar ) ;
parser.addParameter( 'verbose', true, ...
@(x) isnumeric(x) || islogical(x) ) ;
parser.parse( varargin{:} ) ;
args = parser.Results ;
if args.verbose
fprintf( '...' ) ;
end
if ~iscell( args.data )
args.data = num2cell( args.data ) ;
end
...
end
You see how simple it is. The first argument of calls to the add methods is the field name in the output struct, and also the parameter name for name/value pairs (declared with addParameter ). The "middle" argument is the default value for addOptional and addParameter, and the last is an optional validator. You can pass a handle on any existing function, e.g. @ischar, define an anonymous function ( @(x) func1(x) || func2(x) && .. ), but you also have the total freedom to pass a handle on any function of yours, e.g. internal or nested.
You can also choose to have some required parameters not parsed
function foo( userId, varargin )
...
parser.parse( varargin{:} ) ;
end
or parsed but still named explicitly:
function foo( filename, varargin )
...
parser.parse( filename, varargin{:} ) ;
end

Sign in to comment.

Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!