MATLAB: How is loading and saving data in MATLAB affected by the compilation process

MATLAB Compiler

I have an application compiled using the MATLAB Compiler. After having compiled the application, I have trouble getting the file I/O operations working in the compiled version because the paths have changed

Best Answer

READING FILES IN THE COMPILED AND UNCOMPILED PROGRAM
1. The MATLABPATH environment variable determines where MATLAB looks for files (in addition to the current working directory). A version of the MATLABPATH exists for both the uncompiled and compiled version of your program.
2. When you compile with the MCC command, if
a. a directory is on your MATLABPATH
b. and it includes a file that is added to your CTF archive,
then the MATLABPATH seen by your compiled MATLAB files is adjusted to include the corresponding directory in the expanded CTF archive.
This generally means that all files that are "visible" in your uncompiled program--other MATLAB files that are called, for instance--are also visible in your compiled program.
In your case it also means that the 'default' directory in your CTF is on the MATLABPATH for your compiled program.
3. This implies that you are able to open your settings file using the command:
[user_defaults]=xlsread('foo.xls');
even in your compiled program, because it is on the MATLABPATH. The command:
[user_defaults]=xlsread('default\foo.xls');
also works, though it is unneccessary. In this case, MATLAB is only searching through the MATLABPATH entries that end in 'default' (as opposed to 'default' subdirectories of the MATLABPATH entries--however, the default subdirectory of the working directory is searched).
WRITING FILES IN THE COMPILED AND UNCOMPILED PROGRAM
4. One thing that is likely to change between the uncompiled and compiled versions of your program is the "working directory". The working directory is the directory from which the program is launched.
When running the uncompiled version of your program, it is likely that the working directory is the one that contains your main function, lets say:
lapsim_results_analyzer_v1_0_export.m
It could be something else if the directory that contains this file was on your MATLABPATH and you invoked this function from another directory.
When running the compiled version of your program, it is mostly likely that the working directory is the one that contains your executable, lets say:
LapSim_Results_Analyzer.exe
5. Besides its role in locating files (which did not come into play above), the working directory serves as the root of partially-qualified filenames (those that do not have a complete path, starting with the drive). That is, when you attempt to write a file using the syntax:
success=xlswrite('export_user_defaults.xls',...
filedata,'B1:B16');
MATLAB attempts to write 'export_user_defaults.xls' in the working directory. Because of the fact that the current working directory changes between the compiled and uncompiled version, the location will be different.
6. There is an asymmetry between read and write operations: The two statements:
[user_defaults]=xlsread('export_user_defaults.xls');
success=xlswrite('export_user_defaults.xls',...
filedata,'B1:B16');
do not necessarily refer to the same 'export_user_defaults.xls', since the first may be reading from a directory on the MATLABPATH, while the second is always writing in the current directory. This accounts for some of the behavior while reading from one place and writing elsewhere.
7. When you attempt to write a file using the syntax:
success=xlswrite('default\export_user_defaults.xls',...
filedata,'B1:B16');
MATLAB attempts to write 'export_user_defaults.xls' in the 'default' subdirectory of the working directory.
If this directory does not exist, the directory is not created. Instead, the write operation is not successful. This seems to be what was happening in your original version of the program.
THE CTF AS RECEPTICAL FOR DATA
8. Because of the how compilation changes the location of directories relative to the current working directory, the expanded CTF archive is generally a difficult place to store data from the program.
9. The preferable solution is to not have your compiled program store any data into the CTF archive--treat it as read-only. Instead, distribute the modifiable data alongside the EXE-file and CTF archive.
10. If you must store data back into the CTF archive, you could use the WHICH function to determine the full path of the file you have read (instead of giving the path into the MCR).
For instance, if the following code:
user_defaults = xlsread('export_user_defaults.xls');
locates the appropriate file, then ,
p = which('export_user_defaults.xls');
will store its fully-qualified path (i.e. starting with the drive letter) in "p". This can then be used when issuing your "save" command:
success = xlswrite(p, filedata, 'B1:B16');
More information on this function can be found at:
<http://www.mathworks.com/access/helpdesk/help/techdoc/ref/which.html>