spm_file_split.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function Vo = spm_file_split(V, odir)
  2. % Convert a 4D volume file into a series of 3D volume files
  3. % FORMAT Vo = spm_file_split(V, odir)
  4. % V - filename or spm_vol struct
  5. % odir - output directory [default: same as input]
  6. %
  7. % Vo - spm_vol struct array of output files
  8. %__________________________________________________________________________
  9. % Copyright (C) 2009-2018 Wellcome Trust Centre for Neuroimaging
  10. % Guillaume Flandin
  11. % $Id: spm_file_split.m 7391 2018-08-13 09:55:17Z guillaume $
  12. if ~nargin
  13. [V, sts] = spm_select(1,'nifti','Select a 4D volume file to split');
  14. if ~sts, return; end
  15. end
  16. if ischar(V)
  17. V = spm_vol(spm_file(V,'number',''));
  18. end
  19. [p,n,e] = spm_fileparts(V(1).fname);
  20. if nargin < 2
  21. if isempty(p), p = pwd; end
  22. odir = p;
  23. end
  24. Voo = cell(numel(V),1);
  25. spm_progress_bar('Init',numel(V),'Splitting 4D Volume','Volumes Complete');
  26. for i=1:numel(V)
  27. Voo{i} = fullfile(odir,sprintf('%s_%05d%s',n,i,e));
  28. ni = nifti;
  29. ni.dat = file_array(Voo{i},V(i).dim(1:3),V(i).dt,0,V(i).pinfo(1),V(i).pinfo(2));
  30. ni.mat = V(i).mat;
  31. ni.mat0 = V(i).mat;
  32. ni.mat_intent = V(i).private.mat_intent;
  33. ni.mat0_intent = V(i).private.mat0_intent;
  34. ni.descrip = [V(i).descrip sprintf(' - %d',i)];
  35. create(ni);
  36. ni.dat(:,:,:) = V(i).private.dat(:,:,:,i);
  37. spm_progress_bar('Set',i);
  38. end
  39. spm_progress_bar('Clear');
  40. if nargout
  41. Vo = spm_vol(char(Voo));
  42. end