from
3D Cube Slice
by Oren Rosen
Enables reading 2 dim slices of 3 dim matrix stored in MAT file
|
| ReadCubeSlice_orig(matfilename,dim,indx)
|
function slc = ReadCubeSlice_orig(matfilename,dim,indx)
% Oren Rosen
% The MathWorks
% 11/01/2006
%
% This function will read individual 2-dim slices of a 3-dim matrix stored
% in the MAT file 'matfilename' without loading the whole file into MATLAB
% memory. The user specifices which dimension to fix ( dim = 'x','y','z' )
% and which particular slice ( indx = 1,2,.. ect.)
%
% NOTE: THIS REQUIRES THAT THE MAT FILE IS SAVED UNCOMPRESSED USING THE -v6
% OPTION!!!
%
% This function makes use of the MATLAB central function where.m written by
% Malcolm Lidierth. This function provides critical information regarding
% memory allocation and positioning of data within the MAT file.
%
% This function is the first attempt at solving this problem. Step 3 uses
% a for loop and repeated calls to fseek/fread to extract data. This is
% very inefficient. See ReadCubeSlice() for a much better method.
% Step 1: Use 'where' to get information regarding data memory positioning
% within the MAT file and dimensions of data
mem_info = where(matfilename);
% 'offset' is relative position of the actual data within the MAT file.
offset = mem_info.DataOffset{1}.DiscOffset;
% 'datatype' is numeric type of numbers in 'dataset'. Need this for fseek.
datatype = [mem_info.DiscClass{1} '=>' mem_info.class];
% Get dimension info of 3-d data.
dimx = mem_info.size(1);
dimy = mem_info.size(2);
dimz = mem_info.size(3);
% Step 2: Create matrix of absolute indices within 3-d data
% of the 2-d slice requested. Will need this to selectively read
% through MAT file.
slc_indx = slice_index_set(dim,indx,dimx,dimy,dimz);
slc = zeros(size(slc_indx));
num_elements = numel(slc);
% Step 3: Read data from MAT file.
fid = fopen(matfilename,'r');
% The 2nd argument of fseek determines where in the file data is read.
% This is specified in bytes. Accessing the correct element requires
% knowing how many bytes each requires for storage.
%
% NOTE: The use of a for loop and repeated calls to fseek/fread is
% inefficient. See ReadCubeSlice2() for a much better method.
num_bytes = sizeof(mem_info.DiscClass{1});
for i=1:num_elements
fseek(fid,offset+num_bytes*(slc_indx(i)-1),'bof');
slc(i) = fread(fid,1,datatype);
end
fclose(fid);
|
|
Contact us at files@mathworks.com