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