Search code examples
matlabmatlab-guidematlab-compilermatlab-coder

How can I deploy a matlab code that saves .mat files files to communicate between functions, and saves data in the local directory


I wrote a GUI in matlab using GUIDE and I am using a data structure that is saved as a .mat file in the local directory of the matlab application. the .mat file is used to transfer variables between different functions in the GUI, for example, in the beginning of a function I would write

load data;

And I would have access to all the variables stored before. I am now trying to create a standalone application using the matlab compiler, however, the application does not seem to save the matrix in the local directory or any of the results in .mat format. Is there any workaround? I need the .mat functionality as I analyze videos and save each frame data in a seperate .mat file in a folder that I create in the local directory as well.


Solution

  • When you run a compiled MATLAB application, the .exe file unpacks itself into a temporary folder, and is run there. Within the application, you can access the folder to which it's unpacked using the command ctfroot.

    You can refer to folders relative to the output of this command, e.g. matfilefolder = fullfile(ctfroot, 'matfiles').

    Alternatively, you might find it easier, or more maintainable, to use a temporary folder to store your files. You can get the path to the system's temporary folder with the command tempdir, and then create a subfolder within that.

    If you need your application to behave differently when run in MATLAB vs when compiled, you can use the command isdeployed, which will return true when compiled and false otherwise, and then switch behaviour based on the output.