Read NIFTI in fmriStat.

written

If you have a need to run fmristat these days, it can be frustrating that it cant read Nifti files. Using the libraries in surfStat, I added this functionality to fmriStat.

First, you will need to install fmriStat and surfStat from Keith Worsleys website. Otherwise, updating fmristat wouldnt be that useful… ;-)

I overwrote a few files to call on the code from fmristat to read and write nifti files. You can download these files by clicking the link on the top-right, and simply replace the existing fmristat files. Sadly, it is that kludgy.

Read Nifti codec:utf8fmris_read_image.mdownload
function [d] = fmris_read_image(file,Z,T)

% (c) John Aston & Roger Gunn

file = deblank(file);
if ~(file(1)=='/' | file(2)==':')
   if file(1:2)=='./'
      file=[pwd file(3:length(file))];
   else
      file=[pwd '/' file];
   end
end
   
[path,name,ext]=fileparts(file);
if strcmp(ext,'.gz')
   if isunix
      unix(['gunzip ' file]);
   else
      ['Can''t gunzip on non-unix system']
      return
   end
   [path,name,ext]=fileparts(name)
end 

switch lower(ext)
case '.mnc'
   if nargin == 3
      d = fmris_read_minc(file,Z,T);
   else
      d = fmris_read_minc(file);
   end
case '.img'
   if nargin == 3
      d = fmris_read_analyze(file,Z,T);
   else
      d = fmris_read_analyze(file);
   end
case '.brik'
   if nargin == 3
      d = fmris_read_afni(file,Z,T);
   else
      d = fmris_read_afni(file);
   end
case '.nii'
   if nargin == 3
      d = SurfStatReadVol1(file,Z,T);
   else
      d = SurfStatReadVol1(file);
   end

otherwise
   ['Unknown file extension']
end

d.parent_file=file;

return
Write Nifti codec:utf8fmris_write_image.mdownload
function [d]=fmris_write_image(d,Z,T)

% (c) John Aston & Roger Gunn

file = deblank(d.file_name);
if ~(file(1)=='/' | file(2)==':')
   if file(1:2)=='./'
      file=[pwd file(3:length(file))];
   else
      file=[pwd '/' file];
   end
end

[base,ext]=fileparts2(file);
d.file_name=[base ext];
   
switch lower(ext)
case '.mnc'
   if nargin == 3
      fmris_write_minc(d,Z,T);
   else
      fmris_write_minc(d);
   end
case '.img'
   if nargin == 3
      fmris_write_analyze(d,Z,T);
   else
      fmris_write_analyze(d);
   end
case '.brik'
   if nargin == 3
      fmris_write_afni(d,Z,T);
   else
      fmris_write_afni(d);
   end
case '.nii'
   if nargin == 3
      SurfStatWriteVol1(d,Z,T);
   else
      SurfStatWriteVol1(d);
   end
otherwise
   ['Unknown file extension']
end

return

Feel free to contact me if you have any questions! :-Drew