0001 function ok = write_hdr_raw(fname,hdr,be)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
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
0063
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