📄 hdr_savevoxeldata.m
字号:
function success = hdr_SaveVoxelData(hfile)
% HDR::SaveVoxelData - save Analyze image voxel data
%
% FORMAT: hdr.SaveVoxelData;
%
% No input/output fields.
% Version: v0.7b
% Build: 7083116
% Date: Aug-31 2007, 4:35 PM CEST
% Author: Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools
% argument check
if nargin ~= 1 || ...
numel(hfile) ~= 1 || ...
~isBVQXfile(hfile, 'hdr')
error( ...
'BVQXfile:BadArgument', ...
'Invalid call to %s.', ...
mfilename ...
);
end
% get HDR file name -> IMG file
hdrs = bvqxfile_getscont(hfile.L);
hdrc = hdrs.C;
fflp = hdrs.F;
% look in same folder
[ffpn, fflp] = fileparts(fflp);
ifname = [ffpn filesep fflp '.img'];
ifnamc = [ffpn filesep fflp '.IMG'];
if ~exist(ifname, 'file') == 2 && ...
exist(ifnamc, 'file') == 2
ifname = ifnamc;
end
% get data size and type
try
dsize = hdrc.ImgDim.Dim(2:1 + hdrc.ImgDim.Dim(1));
tsize = prod(dsize);
catch
error( ...
'BVQXfile:BadFileContent', ...
'Analyze ImgDim.Dim field error.' ...
);
end
dtype = hdrc.ImgDim.DataType;
if isempty(dtype)
dtype = 0;
end
endian = hdrs.S.EncodingSyntax;
if dtype > 255
dtype = fix(dtype / 256);
switch lower(endian)
case {'ieee-le'}
endian = 'ieee-be';
case {'ieee-be'}
endian = 'ieee-le';
otherwise
error( ...
'BVQXfile:InternalError', ...
'Bad machine datatype/encoding syntax combination.' ...
);
end
end
[tmat, stype] = analyzetype(dtype);
stype = splittocell(stype, '=');
stype = stype{1};
% occupy mem
try
tmat = reshape(hdrc.VoxelData(:), [tsize, 1]);
catch
error( ...
'BVQXfile:InvalidArraySize', ...
'Invalid VoxelData array size.' ...
);
end
% open image file
try
fid = [];
fid = fopen(ifname, 'wb', endian);
fseek(fid, 0, 'bof');
if hdrc.ImgDim.VoxOffset > 0
fwrite(fid, ...
char(zeros(1, floor(hdrc.ImgDim.VoxOffset))), ...
'char');
end
catch
try
fclose(fid);
catch
% nothing
end
error( ...
'BVQXfile:FileNotReadable', ...
'Error opening/writing image file %s.', ...
ifname ...
);
end
% write data to voxeldata file
try
success = (fwrite(fid, tmat, stype) == tsize);
catch
success = false;
warning( ...
'BVQXfile:ErrorWritingData', ...
'Couldn''t write image data into file %s.', ...
ifname ...
);
end
fclose(fid);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -