Using isdeployed to avoid un-supported features with Application Compiler
5 views (last 30 days)
Show older comments
I'm using the Application Compiler to compile my code into a standalone executable. I have some features in my code that cannot be compiled due to the use of un-supported functions/classes/types (e.g. Simulink objects). I'm trying to get around this by wrapping the parts of the code that use un-supported functions in if isdeployed blocks. The un-supported features use hundreds of .m files, but can be disabled by a single if isdeployed block at the top level.
When I open Application Compiler and add my main function, it auto-detects all of the "Files required for your application to run". This auto-detection does not respect or understand the if isdeployed block. The application compiles & runs, but the problem is that there are many extra & unnecessary files included in the compilation process.
How can I tell the Application Compiler to NOT include any files that won't actually be used in the compiled code? These are 2 ways I've come up with, and neither is great.
- Manually remove each un-needed .m file. Lots of manual work.
- Comment out the feature (instead of using an if isdeployed block). Not ideal because I can't have the same code in my git repo that does both in-matlab with the extra feature and compiled without the feature.
Here's an example. This is the main file that is compiled:
function [out] = compileMe(in)
% Test function
out = sprintf("I got %s", in);
if isdeployed
tmp = helperFun();
end
end
And here is the helperFun, which should not be included in the compilation:
function [tmp] = helperFun()
% Not supported in Matlab Compiler
tmp = Simulink.Block;
end
This is what I get by running the Application Compiler:

While there is just 1 "helperFun" in this example, in my real code there are hundreds of .m files here that would be annoying to remove manually.
1 Comment
Walter Roberson
on 2 Apr 2025
Note that the long-term solution to this is to use Simulink Compiler, which does support compiling mixes of MATLAB and Simulink.
Accepted Answer
Aravind
on 8 Apr 2025
The Application Compiler performs a dependency analysis to identify which files are referenced or used by the main file you provide. This analysis is performed without executing the code, so the Application Compiler does not have knowledge of the value of the "isdeployed" variable, which means it cannot selectively exclude certain files. As a result, all files identified by the dependency analyzer are included in the "Files required for your application to run" section.
To exclude specific files or functions from dependency analysis, you can use the “%#exclude” pragma. This allows you to prevent unnecessary files or functions from being included without manually removing each one or commenting out features. Here is an example of how to use the pragma:
if ~isdeployed
%#exclude unsupportedFunction
unsupportedFunction();
end
By implementing this in your code for all unsupported functions, you can prevent them from being discovered during dependency analysis, ensuring they are not included when compiling the application. You can find more information about the “%#exclude” pragma on the following documentation page: https://www.mathworks.com/help/releases/R2024a/compiler/exclude.html.
I hope this helps resolve your issue.
0 Comments
More Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!