Home > rest_20090422 > rest_spm5_files > @read_nifti > private > write_hdr_raw.m

write_hdr_raw

PURPOSE ^

Write a NIFTI-1 .hdr file.

SYNOPSIS ^

function ok = write_hdr_raw(fname,hdr,be)

DESCRIPTION ^

 Write a NIFTI-1 .hdr file.
 FORMAT ok = write_hdr_raw(fname,hdr,be)
 fname - filename of image
 hdr   - a structure containing hdr info
 be    - whether big-endian or not
 ok    - status (1=good, 0=bad)
 _______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscience

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

write_hdr_raw.m

SOURCE CODE ^

0001 function ok = write_hdr_raw(fname,hdr,be)
0002 % Write a NIFTI-1 .hdr file.
0003 % FORMAT ok = write_hdr_raw(fname,hdr,be)
0004 % fname - filename of image
0005 % hdr   - a structure containing hdr info
0006 % be    - whether big-endian or not
0007 % ok    - status (1=good, 0=bad)
0008 % _______________________________________________________________________
0009 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
0010 
0011 %
0012 % $Id: write_hdr_raw.m 253 2005-10-13 15:31:34Z guillaume $
0013 
0014 
0015 [pth,nam,ext] = fileparts(fname);
0016 
0017 if isfield(hdr,'magic')
0018     org = niftistruc;
0019     switch deblank(hdr.magic)
0020     case {'ni1'}
0021         hname = fullfile(pth,[nam '.hdr']);
0022     case {'n+1'}
0023         hname = fullfile(pth,[nam '.nii']);
0024     otherwise
0025         error('Bad header.');
0026     end;
0027 else
0028     org   = mayostruc;
0029     hname = fullfile(pth,[nam '.hdr']);
0030 end;
0031 
0032 if nargin >=3
0033     if be, mach = 'ieee-be';
0034     else   mach = 'ieee-le';
0035     end;
0036 else       mach = 'native';
0037 end;
0038 
0039 ok  = true;
0040 fp  = fopen(hname,'r+',mach);
0041 if fp==-1
0042     fp  = fopen(hname,'w+',mach);
0043     if fp==-1
0044         ok  = false;
0045         return;
0046     end;
0047 end;
0048 
0049 for i=1:length(org)
0050     if isfield(hdr,org(i).label),
0051         dat = hdr.(org(i).label);
0052         if length(dat) ~= org(i).len,
0053             if length(dat)< org(i).len,
0054                 dat = [dat(:) ; zeros(org(i).len-length(dat),1)];
0055             else
0056                 dat = dat(1:org(i).len);
0057             end;
0058         end;
0059     else
0060         dat = org(i).def;
0061     end;
0062     % fprintf('%s=\n',org(i).label)
0063     % disp(dat)
0064     len = fwrite(fp,dat,org(i).dtype.prec);
0065     if len ~= org(i).len,
0066         ok = false;
0067     end;
0068 end;
0069 fclose(fp);
0070 if ~ok,
0071      fprintf('There was a problem writing to the header of\n');
0072      fprintf('"%s"\n', fname);
0073 end;
0074 return;
0075

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005