parseCommand.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. % parseCommand
  2. %
  3. % Parses out a string in the format specified below, into a structure.
  4. %
  5. % CommandType:ParamName1=ParamValue1;ParamName2=ParamValue2;
  6. %
  7. % The command string can have as many parameters in it as possible.
  8. %
  9. % Example:
  10. % ExpParameters:TaskType=Disc;StimPattern=0;SRad=10.0000;PRad=30.0000;Int
  11. % =0.2000;Xpos=0.0000;Ypos=5.0000;Dur=0.2000;
  12. %
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14. % Kian Torab
  15. % kianabc@kianabc.com
  16. % Department of Bioengineering
  17. % University of Utah
  18. % Contributors:
  19. %
  20. % Version 1.3.0.0 - November 2, 2010
  21. % Last edit by: Kian Torab
  22. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  23. function [parsedCommand errorFlag] = parseCommand(inputString)
  24. errorFlag = 0;
  25. inputString(find(inputString == '#')) = [];
  26. colonPos = find(inputString == ':', 1);
  27. parsedCommand.Type = inputString(1:colonPos-1);
  28. inputString = inputString(colonPos+1:end);
  29. try
  30. splitString = regexp(inputString(1:end), ';', 'split');
  31. splitString(end) = [];
  32. if isempty(inputString(find(inputString == '=')))
  33. parsedCommand.Value = splitString{:};
  34. else
  35. splitParams = regexp(splitString', '=', 'split');
  36. splitParams = reshape([splitParams{:}], 2, length(splitString))';
  37. if isempty(splitString)
  38. parsedCommand = [];
  39. errorFlag = 1;
  40. else
  41. for idx = 1:length(splitString)
  42. [numVal, OK] = str2num(splitParams{idx, 2}); %#ok<ST2NM>
  43. if OK
  44. parsedCommand.(splitParams{idx,1}) = numVal;
  45. else
  46. parsedCommand.(splitParams{idx,1}) = splitParams{idx, 2};
  47. end
  48. end
  49. end
  50. end
  51. catch
  52. disp(['Cannot parse: ' inputString]);
  53. errorFlag = 1;
  54. end