spm_data_read.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. function Y = spm_data_read(V,varargin)
  2. % Read data from disk [Y = V(I)]
  3. % FORMAT Y = spm_data_read(V)
  4. % V - a structure array (see spm_data_hdr_read)
  5. % Y - an array of data values; the last dimension indexes numel(V)
  6. %
  7. % FORMAT Y = spm_data_read(V,'slice',S)
  8. % V - a structure array of image volumes (see spm_data_hdr_read)
  9. % S - an array of slice indices
  10. % Y - an array of data values with dimensions (x,y,s,v)
  11. %
  12. % FORMAT Y = spm_data_read(V,'xyz',XYZ)
  13. % V - a structure array (see spm_data_hdr_read)
  14. % XYZ - a [n x m] array of m coordinates {voxel (n=3 or 4)/vertex (n=1)}
  15. % Y - an array of data values with dimensions (v,m)
  16. %
  17. % FORMAT Y = spm_data_read(V,I1,I2,...)
  18. % V - a structure array (see spm_data_hdr_read)
  19. % I1,I2,...- subscript arrays
  20. % Y - an array of data values with dimensions (v,m)
  21. %__________________________________________________________________________
  22. % Copyright (C) 2012 Wellcome Trust Centre for Neuroimaging
  23. % Guillaume Flandin
  24. % $Id: spm_data_read.m 6486 2015-06-24 16:27:17Z guillaume $
  25. if ~isstruct(V)
  26. V = spm_data_hdr_read(V);
  27. end
  28. cl = class(V(1).private);
  29. if isfield(V(1),'dat') && ~isequal(cl,'gifti'), cl = 'nifti'; end
  30. switch cl
  31. case 'nifti'
  32. if isempty(varargin)
  33. % Y = V.private.dat(); % if numel(V)==1, is faster
  34. Y = spm_read_vols(V);
  35. elseif ischar(varargin{1}) && ~isequal(varargin{1},':')
  36. switch lower(varargin{1})
  37. case 'slice'
  38. for i=1:numel(V), for p=1:numel(varargin{2})
  39. Y(:,:,p,i) = spm_slice_vol(V(i),spm_matrix([0 0 varargin{2}(p)]),V(i).dim(1:2),0);
  40. end, end
  41. if numel(V)==1, Y=Y(:,:,:,1); end
  42. case 'xyz'
  43. Y = spm_get_data(V,varargin{2});
  44. otherwise
  45. error('Unknown input option.');
  46. end
  47. else
  48. indices = varargin;
  49. n = get_ndata(V(1).dim,indices{:});
  50. Y = zeros(numel(V),prod(n));
  51. for i=1:numel(V)
  52. if numel(indices) == 1
  53. ind = {indices{1} + (V(i).n(1)-1)*prod(V(i).dim)};
  54. else
  55. ind = indices;
  56. end
  57. Y(i,:) = reshape(V(i).private.dat(ind{:}),1,[]);
  58. end
  59. end
  60. case 'gifti'
  61. indices = varargin;
  62. if isempty(indices)
  63. indices = repmat({':'},1,ndims(V));
  64. elseif strcmpi(indices{1},'xyz')
  65. indices = {indices{2}(1,:)};
  66. end
  67. n = get_ndata(V(1).dim,indices{:});
  68. Y = zeros(numel(V),prod(n));
  69. for i=1:numel(V)
  70. Y(i,:) = reshape(V(i).private.cdata(indices{:}),1,[]);
  71. end
  72. if isempty(varargin), Y = Y'; end % to be coherent with spm_read_vols
  73. otherwise
  74. error('Unknown data type.');
  75. end
  76. %==========================================================================
  77. function n = get_ndata(dim,varargin)
  78. n = zeros(1,numel(varargin));
  79. for i=1:numel(varargin)
  80. if isequal(varargin{i},':')
  81. if i==numel(varargin)
  82. n(i) = dim(i); %prod(dim(i:end));
  83. else
  84. n(i) = dim(i);
  85. end
  86. else
  87. n(i) = numel(varargin{i});
  88. end
  89. end