Skip to Main Content Skip to Search
Product Documentation

MATLAB Data File (MAT Files)

Explicitly Including MAT files Using the %#function Pragma

MATLAB Compiler excludes MAT files from Dependency Analysis Function (depfun) by default.

If you want MATLAB Compiler to explicitly inspect data within a MAT file, you need to specify the %#function pragma when writing your MATLAB code.

For example, if you are creating a solution with Neural Network Toolbox, you need to use the %#function pragma within your GUI code to include a dependency on the gmdistribution class, for instance.

Load and Save Functions

If your deployed application uses MATLAB data files (MAT-files), it is helpful to code LOAD and SAVE functions to manipulate the data and store it for later processing.

For more information about CTF archives, see Component Technology File (CTF Archive).

See the ctfroot reference page for more information about ctfroot.

Use the following example as a template for manipulating your MATLAB data inside, and outside, of MATLAB.

Using Load/Save Functions to Process MATLAB Data for Deployed Applications

The following example specifies three MATLAB data files:

  1. Navigate to install_root\extern\examples\Data_Handling.

  2. Compile ex_loadsave.m with the following mcc command:

    mcc -mv ex_loadsave.m -a 'user_data.mat' -a
         './userdata/extra_data.mat' -a 
         '../externdata/extern_data.mat'
    

ex_loadsave.m.  

function ex_loadsave
% This example shows how to work with the 
% "load/save" functions on data files in
% deployed mode. There are three source data files
% in this example.
%    user_data.mat 
%    userdata/extra_data.mat 
%    ../externdata/extern_data.mat
%
% Compile this example with the mcc command: 
%     mcc -m ex_loadsave.m -a 'user_data.mat' -a
%     './userdata/extra_data.mat' 
%         -a '../externdata/extern_data.mat'
% All the folders under the current main MATLAB file directory will 
%    be included as
% relative path to ctfroot; All other folders will have the 
%    folder
% structure included in the ctf archive file from root of the 
%     disk drive.
%
% If a data file is outside of the main MATLAB file path,
%     the absolute path will be
% included in ctf and extracted under ctfroot. For example: 
%   Data file  
%     "c:\$matlabroot\examples\externdata\extern_data.mat"
%     will be added into ctf and extracted to
%  "$ctfroot\$matlabroot\examples\externdata\extern_data.mat".
% 
% All mat/data files are unchanged after mcc runs. There is
% no excryption on these user included data files. They are 
% included in the ctf archive.
%
% The target data file is:
%   ./output/saved_data.mat
%   When writing the file to local disk, do not save any files 
%    under ctfroot since it may be refreshed and deleted  
%   when the application isnext started.

%==== load data file =============================
if isdeployed
    % In deployed mode, all file under CTFRoot in the path are loaded
    % by full path name or relative to $ctfroot.
    % LOADFILENAME1=which(fullfile(ctfroot,mfilename,'user_data.mat'));    
    % LOADFILENAME2=which(fullfile(ctfroot,'userdata','extra_data.mat'));
    LOADFILENAME1=which(fullfile('user_data.mat'));
    LOADFILENAME2=which(fullfile('extra_data.mat'));
    % For external data file, full path will be added into ctf;
    % you don't need specify the full path to find the file.
    LOADFILENAME3=which(fullfile('extern_data.mat'));
else
    %running the code in MATLAB
    LOADFILENAME1=fullfile(matlabroot,'extern','examples','compiler',
                                     'Data_Handling','user_data.mat');
    LOADFILENAME2=fullfile(matlabroot,'extern','examples','compiler',
                              'Data_Handling','userdata','extra_data.mat');
    LOADFILENAME3=fullfile(matlabroot,'extern','examples','compiler',
                                      'externdata','extern_data.mat');
end

% Load the data file from current working directory
disp(['Load A from : ',LOADFILENAME1]);
load(LOADFILENAME1,'data1');
disp('A= ');
disp(data1);

% Load the data file from sub directory
disp(['Load B from : ',LOADFILENAME2]);
load(LOADFILENAME2,'data2');
disp('B= ');
disp(data2);

% Load extern data outside of current working directory
disp(['Load extern data from : ',LOADFILENAME3]);
load(LOADFILENAME3);
disp('ext_data= ');
disp(ext_data);

%==== multiple the data matrix by 2 ==============
result = data1*data2;
disp('A * B = ');
disp(result);

%==== save  the new data to a new file ===========
SAVEPATH=strcat(pwd,filesep,'output');
if ( ~isdir(SAVEPATH))
    mkdir(SAVEPATH);
end
SAVEFILENAME=strcat(SAVEPATH,filesep,'saved_data.mat');
disp(['Save the A * B result to : ',SAVEFILENAME]);
save(SAVEFILENAME, 'result'); 

MATLAB Objects

When working with MATLAB objects, remember to include the following statement in your MAT file:

%#function class_constructor 

Using the %#function pragma in this manner forces depfun to load needed class definitions, enabling the MCR to successfully load the object.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS