matthiasm@8: function [posns] = strmatch_multi(keys, strs) matthiasm@8: % STRMATCH_MULTI Find where each key occurs in list of strings. matthiasm@8: % [pos] = strmatch_multi(key, strs) where key is a string and strs is a cell array of strings matthiasm@8: % works like the built-in command sequence pos = strmatch(key, strs, 'exact'), matthiasm@8: % except that pos is the first occurrence of key in strs; if there is no occurence, pos is 0. matthiasm@8: % matthiasm@8: % [posns] = strmatch_multi(keys, strs), where keys is a cell array of strings, matthiasm@8: % matches each element of keys. It loops over whichever is shorter, keys or strs. matthiasm@8: matthiasm@8: if ~iscell(keys), keys = {keys}; end matthiasm@8: nkeys = length(keys); matthiasm@8: posns = zeros(1, nkeys); matthiasm@8: if length(keys) < length(strs) matthiasm@8: for i=1:nkeys matthiasm@8: %pos = strmatch(keys{i}, strs, 'exact'); matthiasm@8: ndx = strcmp(keys{i}, strs); % faster matthiasm@8: pos = find(ndx); matthiasm@8: if ~isempty(pos) matthiasm@8: posns(i) = pos(1); matthiasm@8: end matthiasm@8: end matthiasm@8: else matthiasm@8: for s=1:length(strs) matthiasm@8: %ndx = strmatch(strs{s}, keys, 'exact'); matthiasm@8: ndx = strcmp(strs{s}, keys); matthiasm@8: ndx = find(ndx); matthiasm@8: posns(ndx) = s; matthiasm@8: end matthiasm@8: end matthiasm@8: