Picking up from your idea you should just be able to use
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 )
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 );
Best Answer