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 -

Trackbacks

Search this blog

Tags

Most recent comments

  • 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
  • I actually found the answer! When we save PPT as HTML page, the PPT HTML page uses a lots of html pa… by Anthony on this entry
  • Sorry, I'm afraid I have no idea on that! Tom by Thomas Nichols on this entry

Blog archive

Loading…
Not signed in
Sign in

Powered by BlogBuilder
© MMXIV