I have a function that takes variadic arguments. These arguments are parameter-value pairs, so varargin
is a cell array in which every odd-indexed element is a string (the parameter), but the even-indexed elements can be a string, number, or cell array of strings. I want to find the index of a particular string in varargin
. I have a working solution, but it uses arrayfun
twice; is there a cleaner/faster/more effective way of find a string in such a cell array? The resulting index
will be used to remove that element and the following one from varargin
. I would like to minimize creation of new variables.
str_to_find = 'paramX'
finds = arrayfun(@(i) strfind(varargin{i},str_to_find), 1:length(varargin), 'UniformOutput', 0);
finds2 = arrayfun(@(i) ~iscell(finds{i}) && ~isempty(finds{i}), 1:length(finds));
index = find(finds2==1);
varargin(index)=[];
varargin(index)=[];
Given varargin
is {'paramA', 'valueA', 'paramB', 9, 'paramX', {'z','x','c'}
, then finds
is [] [] [] [] [1] {1x3 cell}
, finds2
is 0 0 0 0 1 0
, and index
is 5
. So, my solution does what I need, but it just seems ugly. I would just use finds2
(i.e., not create index
) to delete that element from varargin
, but I also need to remove the one after it.