Generating Standalone MATLAB Executables with MATLAB Coder
Step-by-step guide to generating standalone executables from MATLAB code using MATLAB Coder, covering prerequisites, the codegen command, configuration options, and verification steps.
24 Jul 2026, 20:03 UTC

Desired Outcome
You have a MATLAB function that performs a computation you need to ship to users who do not have MATLAB installed. The goal is to produce a standalone executable that runs on the target platform without a MATLAB license, using only the MATLAB Compiler Runtime (MCR).
Useful takeaway: codegen with the right configuration flag produces the executable, but you must verify code compatibility and redistribute the MCR matching your MATLAB release.
Necessary Prerequisites
- MATLAB license with MATLAB Coder support activated for your release.
- Target platform compatibility – the generated code targets the same operating system and architecture as the build machine, or you cross‑compile with appropriate flags.
- Compatible code subset – your function must use only features supported by MATLAB Coder (basic arithmetic, matrix indexing, control flow, and built‑in functions that do not depend on toolboxes or dynamic evaluation).
- MATLAB Compiler Runtime (MCR) – you will need to download the MCR version matching your MATLAB release from the MathWorks website and redistribute it with the executable.
Focused Procedure
Step 1 – Prepare the function
Ensure your function file, e.g., myMath.m, contains only Coder‑supported operations. Avoid toolbox functions such as fft or ode45 unless you have the corresponding Coder product add‑on and they are on the supported list.
function y = myMath(u)
%#codegen
y = u.^2 + sum(u);
end
The %#codegen directive tells the software to check for code‑generation compatibility.
Step 2 – Run codegen
Execute the codegen command in a terminal or command prompt with the -config:exe flag to produce a standalone executable. Assume the function lives in the current working directory and takes a single column vector input.
Command (run in a terminal with write permission to the current folder):
codegen -config:exe myMath -args {zeros(3,1)}
Placeholders explained:
myMath– the base name of the function file (without the.mextension).{zeros(3,1)}– specifies the size and class of the input argument used for code‑generation validation; adjust to match your actual input.
Required permissions: write access to the current directory and, if generating a DLL, possibly administrator rights for system‑wide registration.
Step 3 – Configure the build (optional)
If you need a shared library instead of an executable, replace -config:exe with -config:lib. For a dynamic link library on Windows, use -config:dll. Each configuration yields a different entry‑point signature:
| Configuration | Entry Point | Runtime Requirement |
|---|---|---|
-config:exe |
standalone main function |
MCR matching the MATLAB release |
-config:lib |
library initialization function (myMath_init, myMath, myMath_terminate) |
MCR matching the MATLAB release; client code must call the initialization/termination pair |
-config:dll |
DLL entry point (myMath) |
MCR matching the MATLAB release; loaded via platform‑specific dynamic linking |
Expected Checks
- Run the generated executable with the same input data you used in MATLAB and compare outputs within a tolerance defined by your application.
- Execute
codegen -reportand verify that the generated report contains no "Unsupported function" warnings for the functions you use. If warnings appear, consult the MATLAB Coder documentation for approved alternatives or required product add‑ons. - Check that the generated entry‑point signature matches your chosen configuration. For
-config:exe, inspect the producedmyMath_main.cand confirm it defines amainfunction with the expected signatureint main(int argc, char **argv).
Limitations and Practical Recovery
Unsupported functions will cause the build to fail or produce runtime errors. The -report flag is the primary diagnostic tool: it lists each unsupported function and suggests compatible alternatives or product add‑ons. If a build fails, you can revise the MATLAB function to fall back to supported operations and re‑run codegen. Because codegen only writes files to disk and does not modify persistent MATLAB state, there is no automatic rollback mechanism – the original .m file remains unchanged, and you simply re‑execute the command after fixing the source.
Performance considerations: generated C code may not achieve the same speed as hand‑optimized C for MATLAB‑idiomatic constructs such as matrix broadcasting or recursive function calls. Profile the generated executable against your reference MATLAB implementation if performance is critical.
Licensing note: the MCR is redistributable, but commercial distribution of generated executables may require a separate MCR license from MathWorks. Verify your license terms before shipping software to end users.
Verification Checklist
- ✓ Input arguments match the
-argsspecification. - ✓
codegen -reportshows no unsupported-function warnings - ✓ Generated executable produces numerically consistent output compared to MATLAB.
- ✓ MCR version listed in the generated installer matches your MATLAB release.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.