MovPartextImport.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. function MovementRegressors = MovPartextImport(filename, startRow, endRow)
  2. %MovementRegressors = MovPartextImport(filename, startRow, endRow)
  3. %
  4. % Internal Function:
  5. % To read the movement parameter text files off the HCP. Generated by
  6. % Matlab.
  7. %
  8. % SA, UoW, 2017
  9. % srafyouni@gmail.com
  10. %
  11. %
  12. if nargin<=2
  13. startRow = 1;
  14. endRow = inf;
  15. end
  16. formatSpec = '%10s%11s%11s%11s%11s%11s%11s%11s%11s%11s%11s%s%[^\n\r]';
  17. fileID = fopen(filename,'r');
  18. dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
  19. for block=2:length(startRow)
  20. frewind(fileID);
  21. dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
  22. for col=1:length(dataArray)
  23. dataArray{col} = [dataArray{col};dataArrayBlock{col}];
  24. end
  25. end
  26. fclose(fileID);
  27. raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
  28. for col=1:length(dataArray)-1
  29. raw(1:length(dataArray{col}),col) = dataArray{col};
  30. end
  31. numericData = NaN(size(dataArray{1},1),size(dataArray,2));
  32. for col=[1,2,3,4,5,6,7,8,9,10,11,12,13]
  33. % Converts text in the input cell array to numbers. Replaced non-numeric
  34. % text with NaN.
  35. rawData = dataArray{col};
  36. for row=1:size(rawData, 1);
  37. % Create a regular expression to detect and remove non-numeric prefixes and
  38. % suffixes.
  39. regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
  40. try
  41. result = regexp(rawData{row}, regexstr, 'names');
  42. numbers = result.numbers;
  43. % Detected commas in non-thousand locations.
  44. invalidThousandsSeparator = false;
  45. if any(numbers==',');
  46. thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
  47. if isempty(regexp(numbers, thousandsRegExp, 'once'));
  48. numbers = NaN;
  49. invalidThousandsSeparator = true;
  50. end
  51. end
  52. % Convert numeric text to numbers.
  53. if ~invalidThousandsSeparator;
  54. numbers = textscan(strrep(numbers, ',', ''), '%f');
  55. numericData(row, col) = numbers{1};
  56. raw{row, col} = numbers{1};
  57. end
  58. catch me
  59. end
  60. end
  61. end
  62. R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
  63. raw(R) = {NaN}; % Replace non-numeric cells
  64. MovementRegressors = cell2mat(raw);