I want to be able to use varargins like plot does, where you have to write the variable name for the variable being specified before defining a value for it, e.g.
plot(x, y, '--rs', 'LineWidth', 4, 'MarkerSize', 10, 'MarkerEdgeColor', 'b')
where 'LineWidth', 'MarkerSize' and 'MarkerEdgeColor' are the variables being changed and 4, 10 and 'b' are the values being assigned to these variables.
I want to know how they do this, so I can implement it into my own code. Is it just a case of doing
if(~isempty(varargin)) for i = 1:size(varargin, 2) - 1 if(strcmp(varagin{i}, 'LineWidth') LineWidth = varargin{i+1}; elseif(strcmp(varargin{i}, 'MarkerSize') MarkerSize = varargin{i+1}; elseif(strcmp(varargin{i}, 'MarkerEdgeColor') MarkerEdgeColor = varargin{i+1}; end end end
or is there a simpler way of doing this? The above method feels really clunky and long winded, so having a quicker method would be ideal.
Best Answer