puzzles on using mutiple “addOptional” in function definitions
How to use multiple |addOptional| in function definitions?
|addOptional| is to add positional arguments. So I want to use it
in the following way.
* First I define the function and use two
|addOptional| there.
* Then I thought I can call in the following
two ways:
|myfunc(‘Jim’, 12)| and |myfunc(‘Jim’, 12, ‘cm’)|.
However, the latter one throws an error:
_The argument ‘cm’ is a string and does not match any parameter names. It failed validation
for the argument ‘units’._
But I can use as |myfunc(‘Jim’, 12, ‘units’, ‘cm’)|, which seems the second |addOptional| behaves as |addParameter|, no longer like positional arguments. What’s the mistake I made? Did I misunderstand something about |addOptional|? Many thanks!
Here is the definition of |myfunc()|:
function myfunc(name, varargin)
par = inputParser;
defaultHeight = 1;
defaultUnits = ‘inches’;
defaultShape = ‘rectangle’;
expectedShape = {‘square’, ‘rectangle’, ‘parallelogram’};
addRequired(par, ‘name’, @ischar);
addOptional(par, ‘height’, defaultHeight, @isnumeric);
addOptional(par, ‘units’, defaultUnits);
% addParameter(par, ‘shape’, defaultShape, …
% @(x) any(validatestring(x,expectedShape)));
parse(par, name, varargin{:});
disp(par.Results)
———
*Updates*:
I found that if the positional arguments are NOT literal string (maybe called as character array in helpdoc, the type shown by whos is char, e.g. ‘any words’), there would be no problems. For example, use |myfunc(‘Jim’, 3, 4)| which is defined as
function myfunc(name, varargin)
p = inputParser;
addRequired(p, ‘name’);
addOptional(p, ‘i’, 1);
addOptional(p, ‘j’, 2);
parse(p, name, varargin{:});
disp(p.Results)
The MATLAB help doc doesn’t mention anything about the limitations in data types of additional positional arguments. Does it imply this is a bug?
*Info*:
* MATLAB: 2016a
* OS: OS X SierraHow to use multiple |addOptional| in function definitions?
|addOptional| is to add positional arguments. So I want to use it
in the following way.
* First I define the function and use two
|addOptional| there.
* Then I thought I can call in the following
two ways:
|myfunc(‘Jim’, 12)| and |myfunc(‘Jim’, 12, ‘cm’)|.
However, the latter one throws an error:
_The argument ‘cm’ is a string and does not match any parameter names. It failed validation
for the argument ‘units’._
But I can use as |myfunc(‘Jim’, 12, ‘units’, ‘cm’)|, which seems the second |addOptional| behaves as |addParameter|, no longer like positional arguments. What’s the mistake I made? Did I misunderstand something about |addOptional|? Many thanks!
Here is the definition of |myfunc()|:
function myfunc(name, varargin)
par = inputParser;
defaultHeight = 1;
defaultUnits = ‘inches’;
defaultShape = ‘rectangle’;
expectedShape = {‘square’, ‘rectangle’, ‘parallelogram’};
addRequired(par, ‘name’, @ischar);
addOptional(par, ‘height’, defaultHeight, @isnumeric);
addOptional(par, ‘units’, defaultUnits);
% addParameter(par, ‘shape’, defaultShape, …
% @(x) any(validatestring(x,expectedShape)));
parse(par, name, varargin{:});
disp(par.Results)
———
*Updates*:
I found that if the positional arguments are NOT literal string (maybe called as character array in helpdoc, the type shown by whos is char, e.g. ‘any words’), there would be no problems. For example, use |myfunc(‘Jim’, 3, 4)| which is defined as
function myfunc(name, varargin)
p = inputParser;
addRequired(p, ‘name’);
addOptional(p, ‘i’, 1);
addOptional(p, ‘j’, 2);
parse(p, name, varargin{:});
disp(p.Results)
The MATLAB help doc doesn’t mention anything about the limitations in data types of additional positional arguments. Does it imply this is a bug?
*Info*:
* MATLAB: 2016a
* OS: OS X Sierra How to use multiple |addOptional| in function definitions?
|addOptional| is to add positional arguments. So I want to use it
in the following way.
* First I define the function and use two
|addOptional| there.
* Then I thought I can call in the following
two ways:
|myfunc(‘Jim’, 12)| and |myfunc(‘Jim’, 12, ‘cm’)|.
However, the latter one throws an error:
_The argument ‘cm’ is a string and does not match any parameter names. It failed validation
for the argument ‘units’._
But I can use as |myfunc(‘Jim’, 12, ‘units’, ‘cm’)|, which seems the second |addOptional| behaves as |addParameter|, no longer like positional arguments. What’s the mistake I made? Did I misunderstand something about |addOptional|? Many thanks!
Here is the definition of |myfunc()|:
function myfunc(name, varargin)
par = inputParser;
defaultHeight = 1;
defaultUnits = ‘inches’;
defaultShape = ‘rectangle’;
expectedShape = {‘square’, ‘rectangle’, ‘parallelogram’};
addRequired(par, ‘name’, @ischar);
addOptional(par, ‘height’, defaultHeight, @isnumeric);
addOptional(par, ‘units’, defaultUnits);
% addParameter(par, ‘shape’, defaultShape, …
% @(x) any(validatestring(x,expectedShape)));
parse(par, name, varargin{:});
disp(par.Results)
———
*Updates*:
I found that if the positional arguments are NOT literal string (maybe called as character array in helpdoc, the type shown by whos is char, e.g. ‘any words’), there would be no problems. For example, use |myfunc(‘Jim’, 3, 4)| which is defined as
function myfunc(name, varargin)
p = inputParser;
addRequired(p, ‘name’);
addOptional(p, ‘i’, 1);
addOptional(p, ‘j’, 2);
parse(p, name, varargin{:});
disp(p.Results)
The MATLAB help doc doesn’t mention anything about the limitations in data types of additional positional arguments. Does it imply this is a bug?
*Info*:
* MATLAB: 2016a
* OS: OS X Sierra addoptional, inputparser MATLAB Answers — New Questions