MATLAB: How to exclude a file from the automatic dependency analysis, which was introduced in R2016b

dependencyMATLAB CompilerMATLAB Compiler SDKmcc

I am using the Application Compiler to compile a MATLAB function, which loads a MAT file. I want to be able to change the MAT file after the application has been compiled and then load the new parameters. In R2016a, I was able to not add the MAT file in the "Files required for your application to run" but to add it under "Files installed for your end user". Then the file would be loaded at run-time.
In R2016b or later, the MAT file is automatically detected by the dependency analysis and cannot be removed from the list "Files required for your application to run". If I try to remove the file, it will appear there again after a second.
I am compiling the following function:
function test()
% test.mat containing the scalar value "tmp=1"
load('test.mat')
disp(tmp)
and the corresponding MAT file "test.mat" is located in the same folder.
How can I exclude the file "test.mat" from the automatic dependency analysis?

Best Answer

In R2020a and later versions, you can use "%#exclude" pragma to ignore a file or function dependency during dependency analysis while executing the mcc command.
For details, please refer the following documentation.
- %#exclude
In R2019b and earlier versions, please refer the following information:
The changed behavior in R2016b can be explained by a new MATLAB Compiler feature to automatically include dependent files, such as MAT files, text, spreadsheets, images, audio, and video, when packaging standalone applications. Please see the
for further details.
As a consequence, it is not possible to remove a file after it has been detected by the dependency analysis.
However, you can change the command for loading the file to a non-relative path by:
load('./test.mat')
Then the MAT file will not detected by the dependency analyzer and you can load it at run-time.
Please note that this workaround may not work when absolute paths are being used, for example:
data = load('\\networkdrive\project\application\datasets\data.mat')
In these situations, one can use "eval" to hide the load command from the dependency analyzer.
Generally speacking, a best practice to exclude certain dependencies when compiling MATLAB code, is to structure the code such that different paths or functions are used for the deployed application. This can be done by using the ISDEPLOYED
command (see documentation for further details). For example:
if isdeployed()
% deployment specific code
else
% normal code
end