forked from anne-urai/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.m
76 lines (66 loc) · 2.42 KB
/
tokenize.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function [tok] = tokenize(str, sep, rep)
% TOKENIZE cuts a string into pieces, returning the pieces in a cell array
%
% Use as
% t = tokenize(str)
% t = tokenize(str, sep)
% t = tokenize(str, sep, rep)
% where
% str = the string that you want to cut into pieces
% sep = the separator at which to cut (default is whitespace)
% rep = whether to treat repeating seperator characters as one (default is false)
%
% With the optional boolean flag "rep" you can specify whether repeated
% seperator characters should be squeezed together (e.g. multiple
% spaces between two words). The default is rep=1, i.e. repeated
% seperators are treated as one.
%
% See also STRTOK, TEXTSCAN
% Copyright (C) 2003-2010, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id: tokenize.m 7123 2012-12-06 21:21:38Z roboos $
% these are for remembering the type on subsequent calls with the same input arguments
persistent previous_argin previous_argout
if nargin<2
sep = [9:13 32]; % White space characters
end
if nargin<3
rep = false;
end
current_argin = {str, sep, rep};
if isequal(current_argin, previous_argin)
% don't do the processing again, but return the previous values from cache
tok = previous_argout;
return
end
tok = {};
f = find(ismember(str, sep));
f = [0, f, length(str)+1];
for i=1:(length(f)-1)
tok{i} = str((f(i)+1):(f(i+1)-1));
end
if rep
% remove empty cells, which occur if the separator is repeated (e.g. multiple spaces)
tok(cellfun('isempty', tok))=[];
end
% remember the current input and output arguments, so that they can be
% reused on a subsequent call in case the same input argument is given
current_argout = tok;
previous_argin = current_argin;
previous_argout = current_argout;