Code covered by the BSD License  

Highlights from
3D Cube Slice

from 3D Cube Slice by Oren Rosen
Enables reading 2 dim slices of 3 dim matrix stored in MAT file

3DCubeSliceDemo.m
%% This script demonstrates the usage of the function ReadCubeSlice.
% Oren Rosen
% 11/9/06
% The MathWorks
%
% See Readme.doc for a description of the problem we are attempting to
% solve with this script.
%
% The script tests 3 different methods of extracting a single 2-dim slice
% of numeric data from a 3-dim dataset stored as a matrix in an
% umcompressed MAT file.
%
% NOTE: I RECOMMEND CLEARING YOUR WORKSPACE BEFORE RUNNING THIS SCRIPT!
%
% 1) Using ReadCubeSlice_orig()
%
% This function was the first attempt to extract a single slice without
% loading the entire 3-dim Matrix to the workspace. In terms of execution
% time, it is usually slower than reading the whole 3-dim matrix into the
% workspace. For this reason it is included here for the most part to
% demonstrate what NOT to do. However, this method does have some merit in
% the fact that you are still saving on how much memory is being used by
% the workspace to store your data sets.
%
% 2) Using ReadCubeSlice()
%
% This function is the second revision. It is much more efficient due to
% using the "skip" parameter in the underlying fread function. In terms of
% execution time, it is usually the fastest of the 3 methods by far. This
% is the best solution of the for data sets of any reasonably large size.
%
% 3) Direct - Load whole 3-dim Matrix into the workspace.
%
% This is done to compare performance. The whole Matrix is loaded
% into the workspace and the desired slice is extracted from here. It is
% still pretty fast but it ties up alot of available memory to store the
% data. This step takes much longer if alot of workspace memory is already
% taken up (if for example you run this script twice without clearing the
% workspace first). 

%% STEP 0: Declare local variables.
% The dimensions of the underlying data set.
dimx = 250;
dimy = 400;
dimz = 300;


% Specify which 2-dim slice to extract here.
% 'dim' specifies which dimension to slice along.
% 'slice_indx' specifies which slice to extract.
dim = 'z';
slice_indx = 73;
fprintf('\n');

%% STEP 1: Create the data set if it doesnt already exist - store to MAT file.
if ~exist('mydata.mat','file')
    disp('Creating large data set and storing to MAT file. This may take a minute');

    % Make the data random but fairly pleasant to look at.
    data = floor(10*randn(dimx,dimy,dimz))/10;
    
% ************************************************************************
% THIS IS IMPORTANT!! NOTE THAT THE MAT FILE IS SAVED WITH THE -V6 OPTION
% THE MAT FILE WILL NOT BE COMPRESSED.
% THIS IS NECESSARY IF WE WANT TO USE THE "where" FUNCTION IN THE
% "readcubeslice" FUNCTIONS.
% ************************************************************************
    save mydata.mat -v6 data;
    clear data
end

%% STEP 2: Extract slice using ReadCubeSlice_orig.
% Time the process.

disp('1) Extracting slice directly from mydata.mat using ReadCubeSlice_orig');
tic;
slc1 = ReadCubeSlice_orig('mydata.mat',dim,slice_indx);
toc;

%% STEP 3: Extract slice using ReadCubeSlice.
% Time the process.

disp('2) Extracting slice directly from mydata.mat using ReadCubeSlice');
tic;
slc2 = ReadCubeSlice('mydata.mat',dim,slice_indx);
toc;


%% STEP 4: Extract slice by loading entire MAT file.

disp('3) Extracting slice by loading entire data set into workspace');
tic;
load mydata.mat;
slc3_indx = slice_index_set(dim,slice_indx,dimx,dimy,dimz);
slc3 = data(slc3_indx);
toc;


%% Step 5: Check for Errors
if ( ~isequal(slc1,slc2) )
    disp('Alert: slc1 is different than slc2!');
end

if ( ~isequal(slc2,slc3) )
    disp('Alert: slc2 is different than slc3!');
end

if ( ~isequal(slc1,slc3) )
    disp('Alert: slc1 is different than slc3!');
end





Contact us at files@mathworks.com