Home > fmri > fsl > read_avw_img.m

read_avw_img

PURPOSE ^

[img] = READ_AVW_IMG(filename)

SYNOPSIS ^

function img = read_avw_img(filename);

DESCRIPTION ^

  [img] = READ_AVW_IMG(filename)

  Read in an Analyze (or nifti .img) file into either a 3D or 4D
   array (depending on the header information)
  Ouput coordinates for all dimensions start at 1 rather than 0
  Note: automatically detects char, short, long or double formats
  
  See also: READ_AVW, READ_AVW_HDR, SAVE_AVW, SAVE_AVW_HDR, SAVE_AVW_IMG

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function img = read_avw_img(filename);
0002 %  [img] = READ_AVW_IMG(filename)
0003 %
0004 %  Read in an Analyze (or nifti .img) file into either a 3D or 4D
0005 %   array (depending on the header information)
0006 %  Ouput coordinates for all dimensions start at 1 rather than 0
0007 %  Note: automatically detects char, short, long or double formats
0008 %
0009 %  See also: READ_AVW, READ_AVW_HDR, SAVE_AVW, SAVE_AVW_HDR, SAVE_AVW_IMG
0010 
0011 fnimg=strcat(filename,'.img');
0012 
0013 [dims,scales,bpp,endian,datatype] = read_avw_hdr(filename);
0014 fp=fopen(fnimg,'r',endian);
0015 if (datatype==4),
0016   dat=fread(fp,'short');
0017 elseif (datatype==2),
0018   dat=fread(fp,'char');
0019 elseif (datatype==8),
0020   dat=fread(fp,'int');
0021 elseif (datatype==64),
0022   dat=fread(fp,'double');
0023 elseif (datatype==16),
0024    dat=fread(fp,'float32');
0025 end
0026 fclose(fp);
0027 
0028 nvox = prod(dims);
0029 if (length(dat)<nvox),
0030   error('Cannot open image as .img file does not contain as many voxels as the .hdr specifies');
0031 elseif (length(dat)>nvox),
0032   disp('WARNING::truncating .img data as it contains more voxels than specified in the .hdr');
0033   dat = dat(1:nvox);
0034 end
0035 
0036 
0037 if (dims(4)>1),
0038   img = reshape(dat,dims(1),dims(2),dims(3),dims(4));
0039 else
0040   img = reshape(dat,dims(1),dims(2),dims(3));
0041 end
0042 
0043 clear dat;
0044 
0045 %% DEFUNCT FLIPPING
0046 %% flip y dimension to be consistent with MEDx
0047 %img=flipdim(img,2);

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003