spm_load.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. function x = spm_load(f,v,hdr)
  2. % Load text and numeric data from file
  3. % FORMAT x = spm_load(f,v,hdr)
  4. % f - filename (can be gzipped) {txt,mat,csv,tsv,json}
  5. % v - name of field to return if data stored in a structure [default: '']
  6. % or index of column if data stored as an array
  7. % hdr - detect the presence of a header row for csv/tsv [default: true]
  8. %
  9. % x - corresponding data array or structure
  10. %__________________________________________________________________________
  11. % Copyright (C) 1995-2018 Wellcome Trust Centre for Neuroimaging
  12. % Guillaume Flandin
  13. % $Id: spm_load.m 7417 2018-09-14 08:53:46Z guillaume $
  14. %-Get a filename if none was passed
  15. %--------------------------------------------------------------------------
  16. if ~nargin
  17. [f,sts] = spm_select(1,{...
  18. 'mat',... % *.txt, *.mat
  19. '^.*\.csv$','^.*\.csv.gz$',... % *.csv, *.csv.gz
  20. '^.*\.tsv$','^.*\.tsv.gz$',... % *.tsv, *.tsv.gz
  21. '^.*\.json$','^.*\.json.gz$',... % *.json, *.json.gz
  22. });
  23. if ~sts, x = []; return; end
  24. end
  25. if ~exist(f,'file')
  26. error('Unable to read file ''%s''',f);
  27. end
  28. if nargin < 2, v = ''; end
  29. if nargin < 3, hdr = true; end % Detect
  30. %-Load the data file
  31. %--------------------------------------------------------------------------
  32. switch spm_file(f,'ext')
  33. case 'txt'
  34. x = load(f,'-ascii');
  35. case 'mat'
  36. x = load(f,'-mat');
  37. case 'csv'
  38. % x = csvread(f); % numeric data only
  39. x = dsvread(f,',',hdr);
  40. case 'tsv'
  41. % x = dlmread(f,'\t'); % numeric data only
  42. x = dsvread(f,'\t',hdr);
  43. case 'json'
  44. x = spm_jsonread(f);
  45. case 'gz'
  46. fz = gunzip(f,tempname);
  47. sts = true;
  48. try
  49. x = spm_load(fz{1});
  50. catch
  51. sts = false;
  52. end
  53. delete(fz{1});
  54. rmdir(spm_file(fz{1},'path'));
  55. if ~sts, error('Cannot load ''%s''.',f); end
  56. otherwise
  57. try
  58. x = load(f);
  59. catch
  60. error('Unknown file format.');
  61. end
  62. end
  63. %-Return relevant subset of the data if required
  64. %--------------------------------------------------------------------------
  65. if isstruct(x)
  66. if isempty(v)
  67. fn = fieldnames(x);
  68. if numel(fn) == 1 && isnumeric(x.(fn{1}))
  69. x = x.(fn{1});
  70. end
  71. else
  72. if ischar(v)
  73. try
  74. x = x.(v);
  75. catch
  76. error('Data do not contain array ''%s''.',v);
  77. end
  78. else
  79. fn = fieldnames(x);
  80. try
  81. x = x.(fn{v});
  82. catch
  83. error('Invalid data index.');
  84. end
  85. end
  86. end
  87. elseif isnumeric(x)
  88. if isnumeric(v)
  89. try
  90. x = x(:,v);
  91. catch
  92. error('Invalid data index.');
  93. end
  94. elseif ~isempty(v)
  95. error('Invalid data index.');
  96. end
  97. end
  98. %==========================================================================
  99. % function x = dsvread(f,delim)
  100. %==========================================================================
  101. function x = dsvread(f,delim,header)
  102. % Read delimiter-separated values file into a structure array
  103. % * header line of column names will be used if detected
  104. % * 'n/a' fields are replaced with NaN
  105. %-Input arguments
  106. %--------------------------------------------------------------------------
  107. if nargin < 2, delim = '\t'; end
  108. if nargin < 3, header = true; end % true: detect, false: no
  109. delim = sprintf(delim);
  110. eol = sprintf('\n');
  111. %-Read file
  112. %--------------------------------------------------------------------------
  113. S = fileread(f); % spm_file(f,'local','content');
  114. if isempty(S), x = []; return; end
  115. if S(end) ~= eol, S = [S eol]; end
  116. S = regexprep(S,{'\r\n','\r','(\n)\1+'},{'\n','\n','$1'});
  117. %-Get column names from header line (non-numeric first line)
  118. %--------------------------------------------------------------------------
  119. h = find(S == eol,1);
  120. hdr = S(1:h-1);
  121. var = regexp(hdr,delim,'split');
  122. N = numel(var);
  123. n1 = isnan(cellfun(@str2double,var));
  124. n2 = cellfun(@(x) strcmpi(x,'NaN'),var);
  125. if header && any(n1 & ~n2)
  126. hdr = true;
  127. try
  128. var = genvarname(var);
  129. catch
  130. var = matlab.lang.makeValidName(var,'ReplacementStyle','hex');
  131. var = matlab.lang.makeUniqueStrings(var);
  132. end
  133. S = S(h+1:end);
  134. else
  135. hdr = false;
  136. fmt = ['Var%0' num2str(floor(log10(N))+1) 'd'];
  137. var = arrayfun(@(x) sprintf(fmt,x),(1:N)','UniformOutput',false);
  138. end
  139. %-Parse file
  140. %--------------------------------------------------------------------------
  141. if strcmpi(spm_check_version,'octave') % bug #51093
  142. S = strrep(S,delim,'#');
  143. delim = '#';
  144. end
  145. if ~isempty(S)
  146. d = textscan(S,'%s','Delimiter',delim);
  147. else
  148. d = {[]};
  149. end
  150. if rem(numel(d{1}),N), error('Varying number of delimiters per line.'); end
  151. d = reshape(d{1},N,[])';
  152. allnum = true;
  153. for i=1:numel(var)
  154. sts = true;
  155. dd = zeros(size(d,1),1);
  156. for j=1:size(d,1)
  157. if strcmp(d{j,i},'n/a')
  158. dd(j) = NaN;
  159. else
  160. dd(j) = str2double(d{j,i}); % i,j considered as complex
  161. if isnan(dd(j)), sts = false; break; end
  162. end
  163. end
  164. if sts
  165. x.(var{i}) = dd;
  166. else
  167. x.(var{i}) = d(:,i);
  168. allnum = false;
  169. end
  170. end
  171. if ~hdr && allnum
  172. x = struct2cell(x);
  173. x = [x{:}];
  174. end