Passing arguments to varargin
54 views (last 30 days)
Show older comments
Hi
Aesthetic detail:
I have a function using several parameters. These parameters have default values, but can be given as input if desired. I am doing this using varargin in the following format:
function y = my_fun(a,b, varargin)
% Default values
length = 0;
height = 2;
% Loading optional arguments
while ~isempty(varargin)
switch lower(varargin{1})
case 'length'
length = varargin{2};
case 'height'
height = varargin{2};
otherwise
error(['Unexpected option: ' varargin{1}])
end
varargin(1:2) = [];
end
% the actual function
y = a + b + length*height;
end
This works fine if I write the options directly into the function, e.g. my_fun(a,b,'length',2). What I would like to do is something like this:
options = {'length',1,'height',2};
my_fun(3,4,options);
I know that I can make it work by passing
options{:}
instead of just options, but is there a way to avoid that?
Thanks!
0 Comments
Accepted Answer
Adam
on 29 Oct 2014
Edited: Adam
on 29 Oct 2014
Picking up from your idea you should just be able to use
varargin{:}
inside your function after passing in options rather than passing in options{:} to achieve the same effect.
Personally I tend to do this kind of thing using object-oriented programming, but that is just preference as I do most of my Matlab programming OOP now, so just throwing it out there as an alternative that you might find neater or are free to completely ignore :)
e.g. I have loads of what I call Args classes which are simple classes with all public properties, just like a struct really, but properly defined.
As an example (bear in mind I have just typed this straight off the top of my head not in Matlab so apologies if it contains syntax errors, it is mostly to put the idea across).
classdef MyFuncArgs
properties( Access = public )
% All your arguments here with their default values
varA = 7;
varB = [15 20 23];
end
methods
function obj = set.varA( obj, varA )
validateattributes( varA, { 'numeric' }, { 'scalar', 'positive' } );
obj.varA = varA;
end
function obj = set.varB( obj, varB )
validateattributes( varA, { 'double' }, { 'vector', 'numel', 3, '>' 5, '<=' 25' } );
obj.varB = varB;
end
end
end
then my function can just take an object of that class as input, I can do a single validateattributes on it to check it is an object of type MyFuncArgs and all the validation of the parameters was done by the Args class so I know in my function that they are there ready for use.
The default values are programmed into the Args class as shown above in the example so that you can either just pass in a default Args class (or create one inside your function if Args is an optional argument to the function) or you can change only as many parameters as you want from defaults and the Args class set functions will ensure you can only set valid parameters.
Where possible I like to set up these Args classes so that the default values are what I most commonly use. So I have a PlotArgs which I use for a Plotter class I have which sets a colourmap, does interpolation, etc, etc in the manner I usually want it, but I can switch off interpolation, change colourmap, etc in that PlotArgs object.
So for example.
args = MyFuncArgs;
args.varB = [10, 12, 13];
y = my_fun( a, b, args );
0 Comments
More Answers (2)
Sean de Wolski
on 29 Oct 2014
Have you tried inputParser?
It provides a good framework for what you're trying to do.
0 Comments
Ced
on 29 Oct 2014
Edited: Ced
on 29 Oct 2014
1 Comment
Adam
on 29 Oct 2014
Certainly it would provide a good, relatively simple, route into OOP for a first time, but if the solution you have works and you are pressed for time then just run with that.
You can also use a standard struct in place of a class to define the default arguments, it just isn't as neat as a struct has dynamic fields and you can't create a default struct without a function wrapper or similar.
See Also
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!