/matlab/fieldtrip-20101107/fileio/private/tokenize.m

http://github.com/yuval-harpaz/ft_BIU · MATLAB · 76 lines · 25 code · 10 blank · 41 comment · 5 complexity · 18fe27fd20798a7e0fdcd22b75d8f5ab MD5 · raw file

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