April 01, 2011

SPM8 Gem 1: Zero NaN's with the zeronan.m script

Follow-up to SPM99 Gem 3: NaNing zero values from Neuroimaging Statistics Tips & Tools

This was the topic of SPM99 Gem 3, converting NaN’s to zeros. For SPM8, see the following script zeronan.m that will zero NaN’s for you.

-Tom

function ofNm = zeronan(ifNm,val)
% FORMAT ofNm = zeronan(ifNm,val)
% ifNm  - Input filename(s)
% val   - Value to set NaN's to (defaults to zero)
%
% Output:
% ofNm  - Cell array of output filenames.
%
%
% Images have NaN's replaced with zeros, and new images, prefixed with a
% 'z', are created.
%
%________________________________________________________________________
% Based on zeronan.m,v 1.3 2005/10/26 21:58:55 nichols Exp 
% Thomas Nichols, 1 April 2011

if nargin<2, val = 0; end
if nargin<1, ifNm = spm_select(Inf,'image','Select images for NaN->0'); end

if ~iscell(ifNm)
  ifNm = cellstr(ifNm)';
else
  ifNm = ifNm(:)';
end

OtfNm = {};

for fNm = ifNm

  fNm = fNm{:};

  OfNm = ['z' fNm];
  [pth,nm,xt,vol] = spm_fileparts(fNm);
  OfNm = fullfile(pth,['z' nm xt]);

  % Code snippet from John Ashburner...
  VI       = spm_vol(fNm);
  VO       = VI;
  VO.fname = OfNm;
  VO       = spm_create_vol(VO);
  for i=1:VI.dim(3),
    img      = spm_slice_vol(VI,spm_matrix([0 0 i]),VI.dim(1:2),0);
    tmp      = find(isnan(img));
    img(tmp) = val;
    VO       = spm_write_plane(VO,img,i);
  end;

  OtfNm = {OtfNm{:} OfNm};

end

if nargout>0
  ofNm = OtfNm;
end

- One comment

  1. Thomas Nichols

    Thanks to Chris Rorden for pointing out that spm_select should have ‘image’ instead of ‘*img’.

    30 Oct 2011, 20:23


Add a comment

Name
Email
Anti-Spam Question
My t-shirt is red. What colour is my t-shirt?
Anti-Spam Answer
Comment


Your IP address will be recorded. -

You can not use HTML, but you can use our special markup -

Search this blog

Tags

Most recent comments

  • Taylor: I've just quickly scanned the BrainVoyager documentation, and it appears that the Cluster–Le… by Thomas Nichols on this entry
  • Cindy, I can see you didn't like my comments but I didn't get based on what you deemed them outrageo… by Yolanda on this entry
  • Thomas, Could you comment on the applicability of your findings to BrainVoyager's Cluster–Level Stat… by Taylor on this entry
  • Here is a sample of recent corrections from PNAS, collated by Mike Angstadt. Correction for Crockett… by Thomas Nichols on this entry
  • Indrajeet, Mike: Thanks for the references to the other corrections. I've used this information to a… by Thomas Nichols on this entry

Blog archive

Loading…
Not signed in
Sign in

Powered by BlogBuilder
© MMXVI