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.


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)';
  ifNm = ifNm(:)';

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);

  OtfNm = {OtfNm{:} OfNm};


if nargout>0
  ofNm = OtfNm;

- 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

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

Your IP address will be recorded. -

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


Search this blog


Most recent comments

  • … better solution: – save your ppt as pptx – close PP – rename your pptx file, give it a new endin… by Michael on this entry
  • Hi, thanx for that, but doesnt work properly on Powerpoint::Mac 2011. It saves no Images, but the Sl… by Michael on this entry
  • Sorry, I only have a Mac don't know how it'll work at on PC. –Tom by Thomas Nichols on this entry
  • This worked like a charm on my mac! Thanks for posting. Now I'm trying to get it to work on a Window… by Julie on this entry
  • @ Anthony: to hide the side frame– 1. OPEN the frame.htm file in the support files folder in an HTML… by mark m on this entry

Blog archive

Not signed in
Sign in

Powered by BlogBuilder