split: Split a string into cellstr at delimiter locations. CTD Calibration toolbox INPUT: str: string Optional input delimiter: a single character or a cell array of single characters to use as delimiters instead of a space. OUTPUT: theResult: cellstring parsed form the input string DESCRIPTION: This function scans a string for a given delimiter [default is a space ' '] and breaks the string into a cell array of substrings. The delimiter is optionally specified as input and can be multiple characters. If a delimiter is comprised of multiple characters, the default behavior is to search for the entire delimiter as one contiguous block of substrings. If the delimiter is a cellstr with multiple elements. The input string is searched recursively for each element of the delimiter cellstr. EXAMPLE: >> str = 'My first delimited line' >> split(str) CHANGELOG: 12-Aug-2004, Version 1.0 * Initial version. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function theResult = split(varargin) 0002 % split: Split a string into cellstr at delimiter locations. 0003 % 0004 % CTD Calibration toolbox 0005 % 0006 % INPUT: 0007 % str: string 0008 % 0009 % Optional input 0010 % delimiter: a single character or a cell array of single characters to use 0011 % as delimiters instead of a space. 0012 % 0013 % OUTPUT: 0014 % theResult: cellstring parsed form the input string 0015 % 0016 % DESCRIPTION: 0017 % This function scans a string for a given delimiter [default is a space ' '] 0018 % and breaks the string into a cell array of substrings. The delimiter is 0019 % optionally specified as input and can be multiple characters. 0020 % 0021 % If a delimiter is comprised of multiple characters, the default behavior 0022 % is to search for the entire delimiter as one contiguous block of substrings. 0023 % If the delimiter is a cellstr with multiple elements. The input string is searched 0024 % recursively for each element of the delimiter cellstr. 0025 % 0026 % EXAMPLE: 0027 % >> str = 'My first delimited line' 0028 % >> split(str) 0029 % 0030 % 0031 % CHANGELOG: 0032 % 12-Aug-2004, Version 1.0 0033 % * Initial version. 0034 % 0035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0036 0037 error(nargchk(1,2,nargin)) 0038 str = varargin{1}; 0039 if ~ischar(str) 0040 error('Frist input argument must be a character array.') 0041 end 0042 if nargin == 2 0043 delim = varargin{2}; 0044 if ~ischar(delim) && ~ iscellstr(delim) 0045 error('delimiter must be a characer array or cellstr.') 0046 end 0047 else 0048 delim = ' '; 0049 end 0050 0051 % Is the delim a string or cell string? 0052 if ischar(delim) 0053 % Parse the string with one delimiter. 0054 theResult = parse_once(str,delim); 0055 if 0056 0057 0058 0059 return 0060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0061 function theCell = parse_once(str,delim) 0062 % parse_once: Parse one line given one delim 0063 % 0064 i = 1; 0065 remain = str; 0066 while remain 0067 [token,remain] = strtok(remain,delim); 0068 if isempty(remain) 0069 % No match with the given delimiter. 0070 theCell={}; 0071 return 0072 else 0073 theCell{i} = token; 0074 i=i+1; 0075 end 0076 0077 0078 return 0079