Home > analyzePRF > utilities > strsplit.m

strsplit

PURPOSE ^

function f = strsplit(str,pattern)

SYNOPSIS ^

function f = strsplit(str,pattern)

DESCRIPTION ^

 function f = strsplit(str,pattern)

 <str> is a string
 <pattern> (optional) is a string.  default: sprintf('\n').
 
 split <str> using <pattern>.  return a cell vector of string fragments.
 note that we generate beginning and ending fragments.

 example:
 isequal(strsplit('test','e'),{'t' 'st'})
 isequal(strsplit('test','t'),{'' 'es' ''})

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function f = strsplit(str,pattern)
0002 
0003 % function f = strsplit(str,pattern)
0004 %
0005 % <str> is a string
0006 % <pattern> (optional) is a string.  default: sprintf('\n').
0007 %
0008 % split <str> using <pattern>.  return a cell vector of string fragments.
0009 % note that we generate beginning and ending fragments.
0010 %
0011 % example:
0012 % isequal(strsplit('test','e'),{'t' 'st'})
0013 % isequal(strsplit('test','t'),{'' 'es' ''})
0014 
0015 % input
0016 if ~exist('pattern','var') || isempty(pattern)
0017   pattern = sprintf('\n');
0018 end
0019 
0020 % find indices of matches
0021 indices = strfind(str,pattern);
0022 
0023 % do it
0024 cnt = 1;
0025 f = {};
0026 for p=1:length(indices)
0027   temp = str(cnt:indices(p)-1);
0028   f = [f {choose(isempty(temp),'',temp)}];
0029   cnt = indices(p)+length(pattern);
0030 end
0031 temp = str(cnt:end);
0032 f = [f {choose(isempty(temp),'',temp)}];

Generated on Wed 18-Jun-2014 21:47:41 by m2html © 2005