function vol2exp(d, sample, output_file_base, frame) %VOL2EXP writes input for the IRIS Explorer latin module. % % VOL2EXP( INPUT [, SAMPLE [, OUTPUT_FILE_BASE [, FRAME]]] ) % % INPUT is the input .mnc or .img file or structure from % fmris_read_image. % % SAMPLE is a 3 x 3 matrix whose rows are the start, step and stop % indices, and whose columns are the x y and z directions. Default is % [1 1 d.dim(1); 1 1 d.dim(2); 1 1 d.dim(3)] i.e. all the data. % % OUPUT_FILE_BASE: writes INPUT to OUPUT_FILE_BASE.exp. % Default is INPUT minus extension if INPUT is file name. % % FRAME: frame, default=1. if nargin < 3 output_file_base=[]; end if nargin < 4 frame=1; end if ischar(d) input_file=d; d=fmris_read_image(input_file,0,0); numslices=d.dim(3); d=fmris_read_image(input_file,1:numslices,frame); else input_file=[d.file_path d.file_name]; end if isempty(output_file_base) output_file_base=input_file(1:(min(findstr('.',input_file))-1)); end ext=input_file(min(findstr('.',input_file))+(1:3)); isminc= all(lower(ext)=='mnc'); if nargin < 2 sample=[]; end if isempty(sample) sample=[1 1 d.dim(1); 1 1 d.dim(2); 1 1 d.dim(3)] end % For .mnc files, emma flips the slices if the voxel sizes are <0, % so we have to flip the voxel sizes and origin: for i=1:2 if d.vox(i)<0 & isminc d.origin(i)=d.origin(i)+d.vox(i).*(d.dim(i)-1); d.vox(i)=-d.vox(i); end end x=sample(1,1):sample(1,2):sample(1,3); y=sample(2,1):sample(2,2):sample(2,3); z=sample(3,1):sample(3,2):sample(3,3); sizes=[length(x) length(y) length(z)] m=[min(x) min(y) min(z); max(x) max(y) max(z)]; bbox=(m-1).*[d.vox; d.vox]+[d.origin; d.origin] fid=fopen([output_file_base '.exp'],'w'); % first 3 are dimension sizes as 4-byte integers: fwrite(fid,sizes,'int'); % next 6 are bounding box as 4-byte floats: fwrite(fid,bbox,'float'); % rest are the data as 4-byte floats: fwrite(fid,d.data(x,y,z),'float'); fclose(fid) return