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