MATLAB Data Files in Compiled Applications

Explicitly Including MATLAB Data files Using the %#function Pragma

The compiler excludes MATLAB® data files (MAT-files) from dependency analysis by default. See Dependency Analysis.

If you want the 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 Deep Learning Toolbox™, you need to use the %#function pragma within your 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.

  • Use isdeployed to determine if your code is running in or out of the MATLAB workspace.

  • Specify the data file by either using WHICH (to locate its full path name) define it relative to the location of ctfroot.

  • All MAT-files are unchanged after mcc runs. These files are not encrypted when written to the deployable archive.

For more information about deployable archives, see Deployable 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:

  • user_data.mat

  • userdata\extra_data.mat

  • ..\externdata\extern_data.mat

  1. Navigate to matlab_root\extern\examples\compiler\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 deployable 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 deployable archive and extracted under ctfroot. For example: 
%   Data file  
%     "c:\$matlabroot\examples\externdata\extern_data.mat"
%     will be added into deployable archive and extracted to
%  "$ctfroot\$matlabroot\examples\externdata\extern_data.mat".
% 
% All mat/data files are unchanged after mcc runs. There is
% no encryption on these user included data files. They are 
% included in the deployable 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 deployable archive;
    % 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');