0001 function simple_replace(epi_dir,epi_stub,bad_vol,good_vols)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 if nargin < 4
0018 error('Too few arguments')
0019 end
0020
0021 nrep = length(good_vols);
0022
0023 bad_vol_fname = spm_get('Files',epi_dir,sprintf('%s*i%04d.img', epi_stub,bad_vol));
0024 if isempty(bad_vol_fname)
0025 error(sprintf('Could not locate a file in %s starting with %s and volume number %d', epi_dir, epi_stub, bad_vol))
0026 end
0027
0028 Vbad = spm_vol(bad_vol_fname);
0029
0030 good_data = zeros(Vbad.dim(1:3));
0031 nslice = Vbad.dim(3);
0032
0033 for irep = 1:nrep
0034 good_vol_fname = spm_get('Files',epi_dir,sprintf('%s*i%04d.img',epi_stub,good_vols(irep)));
0035
0036 if isempty(good_vol_fname)
0037 error(sprintf('Could not locate a file in %s starting with %s and volume number %d', epi_dir, epi_stub, good_vols(irep)))
0038 end
0039
0040 Vgood = spm_vol(good_vol_fname);
0041 for islice = 1:nslice
0042 good_data(:,:,islice) = good_data(:,:,islice) + ...
0043 spm_slice_vol(Vgood,spm_matrix([0 0 islice]),Vgood.dim(1:2),0);
0044 end
0045 end
0046
0047 good_data = good_data/nrep;
0048
0049
0050 Vbad = spm_write_vol(Vbad,good_data);
0051