Home > vbmeg > functions > device > eeg > brainamp > str_split.m

str_split

PURPOSE ^

STR_SPLIT: Split string.

SYNOPSIS ^

function splStr = str_split( str, delimiters, isCollapse )

DESCRIPTION ^

 STR_SPLIT: Split string.

 USAGE:  str_split( str, delimiters, isCollapse=1 ) : delimiters in an array of char delimiters
         str_split( str )             : default are file delimiters ('\' and '/')

 INPUT: 
    - str        : String to split
    - delimiters : String that contains all the characters used to split, default = '/\'
    - isCollapse : If 1, remove all the empty entries
 
 OUTPUT: 
    - splStr : cell array of blocks found between separators

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function splStr = str_split( str, delimiters, isCollapse )
0002 % STR_SPLIT: Split string.
0003 %
0004 % USAGE:  str_split( str, delimiters, isCollapse=1 ) : delimiters in an array of char delimiters
0005 %         str_split( str )             : default are file delimiters ('\' and '/')
0006 %
0007 % INPUT:
0008 %    - str        : String to split
0009 %    - delimiters : String that contains all the characters used to split, default = '/\'
0010 %    - isCollapse : If 1, remove all the empty entries
0011 %
0012 % OUTPUT:
0013 %    - splStr : cell array of blocks found between separators
0014 
0015 % @=============================================================================
0016 % This software is part of the Brainstorm software:
0017 % http://neuroimage.usc.edu/brainstorm
0018 %
0019 % Copyright (c)2000-2013 Brainstorm by the University of Southern California
0020 % This software is distributed under the terms of the GNU General Public License
0021 % as published by the Free Software Foundation. Further details on the GPL
0022 % license can be found at http://www.gnu.org/copyleft/gpl.html.
0023 %
0024 % FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
0025 % UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
0026 % WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
0027 % MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
0028 % LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
0029 %
0030 % For more information type "brainstorm license" at command prompt.
0031 % =============================================================================@
0032 %
0033 % Authors: Francois Tadel, 2008
0034 
0035 % Default delimiters: file delimiters ('\', '/')
0036 if (nargin < 3) || isempty(isCollapse)
0037     isCollapse = 1;
0038 end
0039 if (nargin < 2) || isempty(delimiters)
0040     delimiters = '/\';
0041 end
0042 % Empty input
0043 if isempty(str)
0044     splStr = {};
0045     return
0046 end
0047 
0048 % Find all delimiters in string
0049 iDelim = [];
0050 for i=1:length(delimiters)
0051     iDelim = [iDelim strfind(str, delimiters(i))];
0052 end
0053 iDelim = unique(iDelim);
0054 
0055 % If no delimiter: return the whole string
0056 if isempty(iDelim)
0057     splStr = {str};
0058     return
0059 end
0060 
0061 % Allocates the split array
0062 splStr = cell(1, length(iDelim)+1);
0063     
0064 % First part (before first delimiter)
0065 if (iDelim(1) ~= 1)
0066     iSplitStr = 1;
0067     splStr{iSplitStr} = str(1:iDelim(1)-1);
0068 else
0069     iSplitStr = 0;
0070 end
0071     
0072 % Loop over all other delimiters
0073 for i = 2:length(iDelim)
0074     if (isCollapse  && (iDelim(i) - iDelim(i-1) > 1)) || ...
0075        (~isCollapse && (iDelim(i) - iDelim(i-1) >= 1))
0076         iSplitStr = iSplitStr + 1;
0077         splStr{iSplitStr} = str(iDelim(i-1)+1:iDelim(i)-1);
0078     end
0079 end
0080     
0081 % Last part (after last delimiter)
0082 if (iDelim(end) ~= length(str))
0083     iSplitStr = iSplitStr + 1;
0084     splStr{iSplitStr} = str(iDelim(end)+1:end);
0085 end
0086     
0087 % Remove all the unused entries
0088 if (iSplitStr < length(splStr))
0089     splStr(iSplitStr+1:end) = [];
0090 end
0091 
0092 
0093 
0094 
0095 
0096 
0097

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005