forked from tsalo/misc-fmri-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split4dTo3d.m
33 lines (27 loc) · 815 Bytes
/
split4dTo3d.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function files = split4dTo3d(fname)
% FORMAT files = split4dTo3d(fname)
%
% Splits a 4D nifti file into a series of 3D nifti files. Needs SPM
% functions to work. If input file is fdata.nii, the output files will have
% filenames like fdata_001.nii, fdata_002.nii, etc.
% Written by "baggy" 20111112.
if (nargin < 1)
[fname, sts] = spm_select;
if (sts == 0)
fprintf('split4dTo3d: Operation cancelled.\n');
return
end
end
vol = spm_vol(fname);
img = spm_read_vols(vol);
sz = size(img);
tvol = vol(1);
tvol = rmfield(tvol, 'private');
tvol.descrip = 'generated by split4dTo3d.m';
[dn, fn, ext] = fileparts(fname);
for ctr = 1:sz(4)
tvol.fname = sprintf('%s%s%s_%.3d%s', dn, filesep, fn, ctr, ext);
files{ctr} = tvol.fname;
spm_write_vol(tvol, img(:, :, :, ctr));
end
end