How do I include a data file with my standalone executable generated with the MATLAB Compiler 4.0 (R14)?

54 views (last 30 days)
I have a data file called mydata.txt that I read within my MATLAB code. This MATLAB code is compiled into a standalone application, but the text file is not automatically included. When running the standalone application, I receive the following error:
File not found.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
Data files read by your MATLAB code are not automatically included with standalone applications by the MATLAB Compiler 4.0 (R14). There are three ways to ensure the data files are distributed to users running your compiled application:
1. Include the data file as part of the CTF archive generated by the MATLAB Compiler. For instance, if you have a function called myfun.m that reads the data file mydata.txt, you could compile this code using the following command:
mcc -m myfun.m -a mydata.txt
The mydata.txt file will now be included in the CTF file, and accessible from the standalone application.
To include multiple files you will need to repeat the -a option, as follows:
mcc -m myfun.m -a mydata1.txt -a mydata2.txt -a mydata3.txt
If you want to get the full path to the included data files in the CTF folder, you can do so by using the function CTFROOT, as in the following example:
function myfun
disp(fullfile(ctfroot, mfilename, 'mydata.txt'));
2. Copy the data file to the same location as your EXE- and CTF-files. Within your code, refer to the file by its filename only, with no additional path information. For example, if you open a data file called mydata.txt using the FOPEN function, use the following command:
fid = fopen ('mydata.txt');
See also the Related Solution referenced at the bottom of the page.
3. Refer to the data file by an absolute path on the system. This is the least desirable method since it relies on your data file being in a particular location on a system. As an example, you can include the data file in a directory called "datafiles". When opening a data file called mydata.txt, which exists in the "datafiles" directory, use the following function call in your code:
fid = fopen ('C:\datafiles\mydata.txt');
When deploying your application, you will need to ensure that a directory called "datafiles" exists on the "C" drive of the computer, and that it contains the file mydata.txt.

More Answers (0)

MathWorks Support

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!