I am writing a wrapper for plot that automates some tasks that I find myself doing frequently.
An example code snippet might look like
function myplot(x,y,varargin)
plot(x,y,varargin{:})
xlabel('x axis')
ylabel('y axis')
end
I’m using Matlab’s varargin to pass additional arguments to plot. However, I find that I might want to pass my own optional arguments in varargin. For example, I might want to write something like
>> myplot(1:10, 1:10, 'r', 'LineWidth', 2, 'legend', {'Series 1'})
to have the function automatically include a legend in the plot – that is, I want to be able to mix my own keyword arguments with the ones that you can supply to plot. Short of writing a full parser for my varargs, is there a way to do this simply and reusably in Matlab?
I’ve tried to use the inputParser object, but that would require me to manually add every possible additional argument to plot (and a default for it) which doesn’t seem ideal.
inputParsermay still be the best choice. You can construct the object for your additional arguments, and lump all the parameterName/parameterValue pairs that you want to pass toplotintoUnmatched.