0001 function write_dir(V,dirname,fstub)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 if ~exist(dirname,'dir')
0015 disp(sprintf('Creating directory: %s', dirname))
0016 unix(['mkdir ' dirname]);
0017 end
0018
0019 dims = size(V);
0020 nvol = dims(4);
0021
0022 disp(sprintf('Writing %d volumes ...', nvol))
0023
0024 for ivol = 1:nvol
0025 outfname = sprintf('%s/%s_i%04d.img', dirname, fstub, ivol);
0026
0027 fid = fopen(outfname, 'wb', 'l');
0028 if fid == -1
0029 error(sprintf('Failed to open file %s for writing', outfname))
0030 end
0031
0032 count = fwrite(fid,V(:,:,:,ivol),'integer*2');
0033 if count ~= prod(dims(1:3))
0034 error(sprintf('Failed to write all data for file %s', outfname))
0035 end
0036
0037 fclose(fid);
0038 end