matthiasm@8: function parts = strsplit(splitstr, str, option) matthiasm@8: %STRSPLIT Split string into pieces. matthiasm@8: % matthiasm@8: % STRSPLIT(SPLITSTR, STR, OPTION) splits the string STR at every occurrence matthiasm@8: % of SPLITSTR and returns the result as a cell array of strings. By default, matthiasm@8: % SPLITSTR is not included in the output. matthiasm@8: % matthiasm@8: % STRSPLIT(SPLITSTR, STR, OPTION) can be used to control how SPLITSTR is matthiasm@8: % included in the output. If OPTION is 'include', SPLITSTR will be included matthiasm@8: % as a separate string. If OPTION is 'append', SPLITSTR will be appended to matthiasm@8: % each output string, as if the input string was split at the position right matthiasm@8: % after the occurrence SPLITSTR. If OPTION is 'omit', SPLITSTR will not be matthiasm@8: % included in the output. matthiasm@8: matthiasm@8: % Author: Peter J. Acklam matthiasm@8: % Time-stamp: 2004-09-22 08:48:01 +0200 matthiasm@8: % E-mail: pjacklam@online.no matthiasm@8: % URL: http://home.online.no/~pjacklam matthiasm@8: matthiasm@8: nargsin = nargin; matthiasm@8: error(nargchk(2, 3, nargsin)); matthiasm@8: if nargsin < 3 matthiasm@8: option = 'omit'; matthiasm@8: else matthiasm@8: option = lower(option); matthiasm@8: end matthiasm@8: matthiasm@8: splitlen = length(splitstr); matthiasm@8: parts = {}; matthiasm@8: matthiasm@8: while 1 matthiasm@8: matthiasm@8: k = strfind(str, splitstr); matthiasm@8: if isempty(k) matthiasm@8: parts{end+1} = str; matthiasm@8: break matthiasm@8: end matthiasm@8: matthiasm@8: switch option matthiasm@8: case 'include' matthiasm@8: parts(end+1:end+2) = {str(1:k(1)-1), splitstr}; matthiasm@8: case 'append' matthiasm@8: parts{end+1} = str(1 : k(1)+splitlen-1); matthiasm@8: case 'omit' matthiasm@8: parts{end+1} = str(1 : k(1)-1); matthiasm@8: otherwise matthiasm@8: error(['Invalid option string -- ', option]); matthiasm@8: end matthiasm@8: matthiasm@8: matthiasm@8: str = str(k(1)+splitlen : end); matthiasm@8: matthiasm@8: end