Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Compiler   

Working with MATLAB Data Files Using Load and Save

If your deployed application uses MATLAB data files (MAT files) to store MATLAB variables, graphics, and other data, it is helpful to code LOAD and SAVE functions if you need to manipulate this data and store it for later processing.

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

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

Example: Using Load/Save Functions To Process MATLAB Data for Deployed Applications

In the following example, three MATLAB data files are specified:

Compile ex_loadsave.m with the following mcc command:

mcc -mvC 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 -mC ex_loadsave.m -a 'user_data.mat' -a
%     './userdata/extra_data.mat' 
%         -a '../externdata/extern_data.mat'
% All the folders under the current main m-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 m-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');
  


Recommended Products

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

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