/core/utils_parsing/SplitSequenceMarkerEntry.m

http://brainstream.googlecode.com/ · MATLAB · 82 lines · 54 code · 0 blank · 28 comment · 11 complexity · 346beb6e6f7a6cdef1fc2e10e5c9da1f MD5 · raw file

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % The BrainStream software is free but copyrighted software, distributed %
  3. % under the terms of the GNU General Public Licence as published by %
  4. % the Free Software Foundation (either version 2, or at your option %
  5. % any later version). See the file COPYING.txt in the main BrainStream %
  6. % folder for more details. %
  7. % %
  8. % Copyright (C) 2009, Philip van den Broek %
  9. % Donders Institute for Brain, Cognition and Behaviour %
  10. % Radboud University Nijmegen, The Netherlands %
  11. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12. function [mrkseq,mrks,sqm,to] = SplitSequenceMarkerEntry(s)
  13. %s={'[mrk1 ?parm1 1?parm2 ;0.05]' , '[mrk2 5?parm3 ?parm4 7?parm5 ;0.099]','[mrk3 *parm6 mrk4 ;0.11]'}
  14. if ischar(s), s = cellstr(s); end
  15. [mrksq,rem] = strtok(s,{'[',';',']'});
  16. to = strtok(rem,{'[',';',']'});
  17. % find first position of parameter section (right after marker name)
  18. % '\d*\?' : start with any number (0 or more) and then a question mark
  19. % '*\S' : start with an asterisk and at least 1 non-space character
  20. mrks = cell(1,length(mrksq));
  21. sqmrem = cell(1,length(mrksq));
  22. sqm = cell(1,length(mrksq));
  23. for n = 1 : length(mrksq)
  24. idx=regexp(mrksq{n},{'\d*\?','*\S'});
  25. idx = sort([idx{:}]);
  26. if isempty(idx)
  27. error('wrong marker sequence input detected, missing items');
  28. end
  29. mrks{n} = regexp(mrksq{n}(1:idx(1)-1),'\S.*\S','match'); % remove leading and tailing spaces
  30. sqmrem{n} = mrksq{n}(idx(1):end);
  31. % (1) to capture the ?-items
  32. % \d* means: arbitrary number of digits (0 times or more)
  33. % \? means: one question mark '?'
  34. % \S* means: arbitrary number of non-whitespace characters (0 times or more)
  35. % (2) to capture the *-items
  36. % * means: start with asterisk (*)
  37. % .* means: arbitrary number of non-white space characters
  38. % \S means: ends with a non-whitespace character
  39. sqm{n} = regexp(sqmrem{n},{'\d*\?\S*','*.*\S'},'match');
  40. sqm{n} = [sqm{n}{:}];
  41. end
  42. % Note: If one of the sqm items contains an asterisk-item, it still needs
  43. % to seperate the name of the end marker (also with allowed spaces in the marker name)
  44. mrkseq = cell(1,length(sqm));
  45. for n = 1 : length(sqm)
  46. mrkseq{n}.parms = [];
  47. if ~isempty(regexp([sqm{n}{:}],'*','match'))
  48. % find name, start and end position of *-parameter
  49. [name,startidx,endidx] = regexp(sqm{n},'*\S*','match','start','end');
  50. if isempty(isempty(name) || isequal(endidx,length(sqm{n})))
  51. error('wrong marker sequence input detected, *-parmameter or end marker for *-parameter not found');
  52. end
  53. if length(name)~=1 || length(startidx)~=1 || length(endidx)~=1
  54. error('wrong marker sequence input detected, multiple *-parmameter items found');
  55. end
  56. mrkseq{n}.marker = char(mrks{n});
  57. [unit,timeout] = get_unitvalue(to{n});
  58. if isempty(timeout), error(['Marker sequence timeout read failure, please check following entry: ' char(s)]); end
  59. mrkseq{n}.timeout = timeout;
  60. mrkseq{n}.parms(end+1).name = sqm{n}{1}(startidx{1}+1:endidx{1});
  61. mrkseq{n}.parms(end).numtocollect= sqm{n}{1}(endidx{1}+1:end);
  62. % remove leading and trailing spaces
  63. %mrkseq{n}.parms(end).numtocollect= regexp(mrkseq{n}.parms(end).numtocollect,'\S.*\S','match');
  64. mrkseq{n}.parms(end).numtocollect= {strtrim(mrkseq{n}.parms(end).numtocollect)};
  65. else
  66. mrkseq{n}.parms = repmat(struct('name',[],'numtocollect',[]),1,0);
  67. for num = 1 : length(sqm{n})
  68. mrkseq{n}.marker = char(mrks{n});
  69. [unit,timeout] = get_unitvalue(to{n});
  70. if isempty(timeout), error(['Marker sequence timeout read failure, please check following entry: ' char(s)]); end
  71. mrkseq{n}.timeout = timeout;
  72. parmidx = regexp(sqm{n}{num},'\?'); % get num to collect (ntc)
  73. mrkseq{n}.parms(end+1).name = sqm{n}{num}(parmidx+1:end);
  74. ntc = regexp(sqm{n}{num}(1:parmidx-1),'\d*','match'); % get num to collect (ntc)
  75. if isempty(ntc)
  76. mrkseq{n}.parms(end).numtocollect = 1;
  77. else
  78. mrkseq{n}.parms(end).numtocollect = str2double(char(ntc));
  79. end
  80. end
  81. end
  82. end