I've been writing a lot of functions lately. I like allowing the user to declare options in my functions using name-value pairs, but I have not found a good clean method of interpreting varargin. Here are two methods I tend to use, but both feel a bit clunky:
% Method 1:
for k = 1:length(varargin) if strcmpi(varargin{k},'fontname') fontname = varargin{k+1}; varargin{k+1}=[]; varargin{k}=[]; endend% Method 2:
nk = 1:length(varargin);for k = 1:length(varargin) if strcmpi(varargin{k},'fontsize') fontsize = varargin{k+1}; nk(k:k+1) = []; endendvarargin = varargin(nk);
Is there a better standard procedure for interpreting a list of varargin arguments?
Best Answer