splitString

PURPOSE ^

splitString Splits a string Perl style

SYNOPSIS ^

function fields = splitString(string,delimiter)

DESCRIPTION ^

splitString Splits a string Perl style

 fields = splitString(string,delimiter)

 string      Either a single string or a cell array of strings
 delimiter   Splitting delimiter

 fields      Either a single cell array of fields or a cell array of cell
             arrays of fields

 Default delimiter is '\s' (whitespace)
 Delimiters are perl regular expression style, e.g. '|' has to be expressed
 as '\|'
 Results are returned in the cell array fields 

 07/14/04 Markus Herrgard

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function fields = splitString(string,delimiter)
0002 %splitString Splits a string Perl style
0003 %
0004 % fields = splitString(string,delimiter)
0005 %
0006 % string      Either a single string or a cell array of strings
0007 % delimiter   Splitting delimiter
0008 %
0009 % fields      Either a single cell array of fields or a cell array of cell
0010 %             arrays of fields
0011 %
0012 % Default delimiter is '\s' (whitespace)
0013 % Delimiters are perl regular expression style, e.g. '|' has to be expressed
0014 % as '\|'
0015 % Results are returned in the cell array fields
0016 %
0017 % 07/14/04 Markus Herrgard
0018 
0019 if (nargin < 2)
0020     delimiter = '\s';
0021 end
0022 
0023 % Check if this is a list of strings or just a single string
0024 if iscell(string)
0025     stringList = string;
0026     for i = 1:length(stringList)
0027         fields{i} = splitOneString(stringList{i},delimiter);
0028     end
0029 else
0030     fields = splitOneString(string,delimiter);
0031 end
0032 
0033 fields = columnVector(fields);
0034 
0035 %%
0036 function fields = splitOneString(string,delimiter)
0037 % Internal function that splits one string
0038 
0039 [startIndex,endIndex] = regexp(string,delimiter);
0040 
0041 if (~isempty(startIndex))
0042 cnt = 0;
0043 for i = 1:length(startIndex)+1
0044     if (i == 1)
0045         if (endIndex(i) > 1)
0046             cnt = cnt + 1;
0047             fields{cnt} = string(1:endIndex(i)-1);    
0048         end
0049     elseif (i == length(startIndex)+1)
0050         if (startIndex(i-1) < length(string))
0051             cnt = cnt + 1;
0052             fields{cnt} = string(startIndex(i-1)+1:end);
0053         end
0054     else
0055         cnt = cnt + 1;
0056         fields{cnt} = string(startIndex(i-1)+1:endIndex(i)-1);
0057     end
0058 end
0059 else
0060     fields{1} = string;
0061 end
0062 
0063 fieldsOut = {};
0064 cnt = 0;
0065 for i = 1:length(fields)
0066     if (~isempty(fields{i}))
0067         cnt = cnt+1;
0068         fieldsOut{cnt} = fields{i};
0069     end
0070 end
0071 fields = fieldsOut;

Generated on Thu 21-Jun-2012 15:39:23 by m2html © 2003