Here's some boilerplate code I use for parsing additional, optional input parameters when I write a matlab function. Usual I structure my matlab function prototypes as follows:
function C = function_name(A,B,varargin)
I comment this prototype with a message that's very useful when issuing help function_name
:
% FUNCTION_NAME This is a high-level description of what function_name takes
% as input, what it does and what it produces as output
%
% C = function_name(A,B)
% C = function_name(A,B,'ParameterName',ParameterValue)
%
% Inputs:
% A num_cols by num_rows matrix of blah blah blah
% B num_cols by num_rows matrix of blah blah blah
% Optional:
% 'ParameterName1' followed by an integer blah blah {1}
% 'ParameterName2' followed by one of 'foo','bar',{'oof'} blah blah
% Output:
% C num_cols by num_rows matrix of blah blah blah
%
I parse the optional inputs with a while loop and switch like this:
% defaults for optional parameters
parameter_value1 = 1;
parameter_value2 = 'oof';
% parse optional input parameters
v = 1;
while v < numel(varargin)
switch varargin{v}
case 'ParameterName1'
assert(v+1<=numel(varargin));
v = v+1;
parameter_value1 = varargin{v};
case 'ParameterName2'
assert(v+1<=numel(varargin));
v = v+1;
parameter_value2 = varargin{v};
otherwise
error('Unsupported parameter: %s',varargin{v});
end
v = v+1;
end
Update: I should probably be using matlab's built-in inputParser class, but it means that parameter names have to match variable names in my code. And variables are stored as fields in the inputParse
instance: e.g. parser.Results.variable_name1
. The advantage though is that it can easily handle input type validation.
Here's an updated custom optional input parser. So far without validation, though it seems clear how to add support via function handles, e.g. @isnumeric
, @islogical
, @() customthing...
.
% default values
variable_name1 = false;
variable_name2 = [1,2,3];
% Map of parameter names to variable names
params_to_variables = containers.Map( ...
{'ParamName1','ParamName2'}, ...
{'variable_name1','variable_name2'});
v = 1;
while v <= numel(varargin)
param_name = varargin{v};
if isKey(params_to_variables,param_name)
assert(v+1<=numel(varargin));
v = v+1;
% Trick: use feval on anonymous function to use assignin to this workspace
feval(@()assignin('caller',params_to_variables(param_name),varargin{v}));
else
error('Unsupported parameter: %s',varargin{v});
end
v=v+1;
end