spm_data_write.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function V = spm_data_write(V,Y,varargin)
  2. % Write data to disk [V(I) = Y]
  3. % FORMAT V = spm_data_write(V,Y)
  4. % V - a structure array (see spm_data_hdr_read)
  5. % Y - an array of data values
  6. %
  7. % FORMAT V = spm_data_write(V,Y,I)
  8. % V - a structure array (see spm_data_hdr_read)
  9. % Y - an array of data values
  10. % I - linear index to data values
  11. %__________________________________________________________________________
  12. % Copyright (C) 2012-2014 Wellcome Trust Centre for Neuroimaging
  13. % Guillaume Flandin
  14. % $Id: spm_data_write.m 5916 2014-03-13 13:15:02Z guillaume $
  15. if isfield(V,'private')
  16. cl = class(V.private);
  17. elseif isfield(V,'dat')
  18. cl = 'struct';
  19. else
  20. error('Unkwown data type.');
  21. end
  22. switch cl
  23. case 'nifti'
  24. if isempty(varargin)
  25. V = spm_write_vol(V,Y);
  26. %S = substruct('()',repmat({':'},1,numel(V.private.dat.dim)));
  27. %V.private.dat = subsasgn(V.private.dat,S,Y);
  28. else
  29. if numel(varargin) == 1
  30. try
  31. V.private.dat(varargin{1}) = reshape(Y,size(varargin{1}));
  32. catch
  33. V.private.dat(varargin{1}) = reshape(Y,size(varargin{1}))';
  34. end
  35. else
  36. error('not implemented yet');
  37. end
  38. end
  39. case 'struct'
  40. if isempty(varargin)
  41. V.dat = Y;
  42. else
  43. if numel(varargin) == 1
  44. V.dat(varargin{1}) = reshape(Y,size(varargin{1}));
  45. else
  46. error('not implemented yet');
  47. end
  48. end
  49. case 'gifti'
  50. if isempty(varargin)
  51. D = V.private.cdata;
  52. D = subsasgn(D,substruct('()',{':'}),Y);
  53. %V.private.cdata = D;
  54. else
  55. try
  56. %V.private.cdata(varargin{1}) = reshape(Y,size(varargin{1}));
  57. V.private.private.data{1}.data(varargin{1}) = reshape(Y,size(varargin{1}));
  58. catch
  59. %V.private.cdata(varargin{1}) = reshape(Y,size(varargin{1}))';
  60. V.private.private.data{1}.data(varargin{1}) = reshape(Y,size(varargin{1}))';
  61. end
  62. end
  63. otherwise
  64. error('Unknown data type.');
  65. end