


STRSPLIT - split a tring of delimiter separated values
This function should be used to split a string of delimiter separated
values. If all values are numerical values the returned matrix is a
double array but if there is one non numerical value a cell array is
returned. You can check this with the iscell() function.
inpstr: string containing delimiter separatede numerical values, eg
3498,48869,23908,34.67
Output: An x by 1 matrix containing the splitted values
Delimiter: optional, if omitted the delimiter is , (comma)
Usage:
output = strsplit(inpstr[,delimiter])

0001 function splittedstring = strsplit(inpstr,delimiter) 0002 %STRSPLIT - split a tring of delimiter separated values 0003 % 0004 % This function should be used to split a string of delimiter separated 0005 % values. If all values are numerical values the returned matrix is a 0006 % double array but if there is one non numerical value a cell array is 0007 % returned. You can check this with the iscell() function. 0008 % inpstr: string containing delimiter separatede numerical values, eg 0009 % 3498,48869,23908,34.67 0010 % Output: An x by 1 matrix containing the splitted values 0011 % Delimiter: optional, if omitted the delimiter is , (comma) 0012 % 0013 % Usage: 0014 % output = strsplit(inpstr[,delimiter]) 0015 % 0016 0017 % mailto: gie.spaepen@ua.ac.be 0018 0019 0020 0021 %Check input arguments 0022 if(nargin < 1) 0023 error('There is no argument defined'); 0024 else 0025 if(nargin == 1) 0026 strdelim = ','; 0027 %Verbose off!! disp 'Delimiter set to ,'; 0028 else 0029 strdelim = delimiter; 0030 end 0031 end 0032 0033 %deblank string 0034 deblank(inpstr); 0035 0036 %Get number of substrings 0037 idx = findstr(inpstr,strdelim); 0038 if size(idx) == 0 0039 disp 'No delimiter in string, inputString is returned'; 0040 splittedstring = inpstr; 0041 else 0042 %Define size of the indices 0043 sz = size(idx,2); 0044 %Define splittedstring 0045 tempsplit = {}; 0046 %Loop through string and itinerate from delimiter to delimiter 0047 for i = 1:sz 0048 %Define standard start and stop positions for the start position, 0049 %choose 1 as startup position because otherwise you get an array 0050 %overflow, for the endposition you can detemine it from the 0051 %delimiter position 0052 strtpos = 1; 0053 endpos = idx(i)-1; 0054 %If i is not the beginning of the string get it from the delimiter 0055 %position 0056 if i ~= 1 0057 strtpos = idx(i-1)+1; 0058 end 0059 %If i is equal to the number of delimiters get the last element 0060 %first by determining the lengt of the string and then replace the 0061 %endpos back to a standard position 0062 if i == sz 0063 endpos = size(inpstr,2); 0064 tempsplit(i+1) = {inpstr(idx(i)+1 : endpos)}; 0065 endpos = idx(i)-1; 0066 end 0067 %Add substring to output: splittedstring a cell array 0068 tempsplit(i) = {inpstr(strtpos : endpos)}; 0069 end 0070 %Flag 0071 isallnums = 1; 0072 %Check is there are NaN values if matrix elements are converted to 0073 %doubles 0074 for i = 1:size(tempsplit,2) 0075 tempdouble = str2double(tempsplit(i)); 0076 if(isnan(tempdouble)) 0077 isallnums = 0; 0078 end 0079 end 0080 %If isallnums = 1 then return a double array otherwise return a cell 0081 %array 0082 if(isallnums == 1) 0083 for i = 1:size(tempsplit,2) 0084 splittedstring(i) = str2double(tempsplit(i)); 0085 end 0086 else 0087 splittedstring = tempsplit; 0088 end 0089 0090 0091 end