MATLAB Codegen Deploy
Use this skill when MATLAB or Simulink algorithms need to become generated code or deployable artifacts.
Workflow
- Identify target: MEX, C/C++, embedded C, CUDA, HDL, PLC, or packaged app.
- Verify required products with
ver and license.
- Isolate the algorithm into a codegen-friendly function.
- Define input types with
coder.typeof or representative test vectors.
- Generate the smallest artifact first, usually MEX.
- Compare generated result against MATLAB golden output.
- Save generated code, logs, reports, and validation metrics.
Self-Compile and Verify Preference
The user expects generated MATLAB/Simulink work to compile and verify itself after writing. Do not stop at source generation when a build or smoke check is feasible:
- For MATLAB Coder, run
codegen and then compare generated/MEX output against MATLAB golden output when compiler support exists.
- For Simulink Coder, update/compile the model first, run simulation, then call
slbuild only after simulation passes.
- For embedded targets such as STM32, first try the configured hardware target and toolchain; if it fails, capture the exact missing package/compiler message and generate the closest portable C fallback if possible.
- Verify generated
.c, .h, project, library, elf/axf/hex/bin, or fallback artifacts exist and are nonempty.
- Report clearly which stage passed: model update, simulation, code generation, toolchain build, binary generation, or only portable fallback.
MATLAB Coder Pattern
Use this pattern before larger deployment:
cfg = coder.config("mex");
codegen -config cfg myFunction -args {coder.typeof(0,[100 1],[1 0])}
gold = myFunction(x);
actual = myFunction_mex(x);
assert(norm(gold-actual) < 1e-9)
Embedded Readiness
Check:
- Fixed-size vs variable-size arrays.
- Dynamic allocation and recursion.
- Unsupported functions.
- Numeric overflow and fixed-point scaling.
- Stack and heap impact.
- Timing budget and hardware target assumptions.
Simulink Codegen
For models:
- Update/compile the model after editing and before
slbuild.
- Build only after simulation passes.
- Set solver and sample times explicitly.
- Run Model Advisor or relevant checks when available.
- Compare simulation outputs before and after code generation.
STM32 Embedded Coder Readiness
For STM32 targets, do not treat "an executable exists on disk" as enough. Verify the full MATLAB-to-ST chain before blaming the model:
- MATLAB products: Simulink, Simulink Coder, Embedded Coder, STM32 support package.
- MATLAB registration:
stm32cube.hwsetup.stm32Tools.getInstalledSTM32CubeMX() returns the CubeMX directory MATLAB will use.
- ST tools: GNU Tools for STM32, CMSIS, CMSIS-DSP, STM32CubeMX, and the matching STM32Cube firmware package such as
STM32Cube_FW_F1_*.
- Model binding: the model has a real
.ioc path in STM32CubeMX.ProjectFile.
- Target hardware:
codertarget.targethardware.getRegisteredTargetHardwareNames includes the required STM32 family, for example STM32F1xx Based.
Prefer target data APIs over editing opaque structures:
tools = stm32cube.hwsetup.stm32Tools();
tools.updateSTM32CubeMXPath("C:\Users\me\AppData\Local\Programs\STM32CubeMX");
codertarget.data.setParameterValue(model, "STM32CubeMX.ProjectFile", iocPath);
codertarget.data.setParameterValue(model, "STM32CubeMX.DeviceId", "STM32F103RBTx");
codertarget.data.setParameterValue(model, "STM32CubeMX.Family", "STM32F1");
If slbuild hangs around "Generating code from STM32CubeMX project" or "Starting compilation process":
- Check
STM32CubeMX.log, generated hardware scripts, and background java.exe / STM32CubeMX.exe processes.
- Compare direct
STM32CubeMX.exe -q script with MATLAB's java -jar STM32CubeMX.exe -q script; some CubeMX versions can behave differently.
- Prefer the MathWorks-recommended CubeMX version shown by
message("stm32:setup:CubeMXReqVersion").getString.
- If non-ASCII paths are involved, copy the model and
.ioc to an ASCII scratch path and retry once to isolate path encoding from model errors.
- Only use a local CubeMX wrapper as a documented workaround after recording the direct failure and the real CubeMX path.
Output
Report target, generated artifact path, report path, golden comparison result, and limitations.
1---2name: matlab-codegen-deploy3description: MATLAB R2026a code generation and deployment workflow for MATLAB Coder, Simulink Coder, Embedded Coder, GPU Coder, HDL Coder, fixed-point conversion, MEX validation, and embedded deployment readiness. Use whenever the user asks to generate C/C++/CUDA/HDL/code, deploy algorithms, or validate generated code.4---56# MATLAB Codegen Deploy78Use this skill when MATLAB or Simulink algorithms need to become generated code or deployable artifacts.910## Workflow11121. Identify target: MEX, C/C++, embedded C, CUDA, HDL, PLC, or packaged app.132. Verify required products with `ver` and `license`.143. Isolate the algorithm into a codegen-friendly function.154. Define input types with `coder.typeof` or representative test vectors.165. Generate the smallest artifact first, usually MEX.176. Compare generated result against MATLAB golden output.187. Save generated code, logs, reports, and validation metrics.1920## Self-Compile and Verify Preference2122The user expects generated MATLAB/Simulink work to compile and verify itself after writing. Do not stop at source generation when a build or smoke check is feasible:23241. For MATLAB Coder, run `codegen` and then compare generated/MEX output against MATLAB golden output when compiler support exists.252. For Simulink Coder, update/compile the model first, run simulation, then call `slbuild` only after simulation passes.263. For embedded targets such as STM32, first try the configured hardware target and toolchain; if it fails, capture the exact missing package/compiler message and generate the closest portable C fallback if possible.274. Verify generated `.c`, `.h`, project, library, `elf/axf/hex/bin`, or fallback artifacts exist and are nonempty.285. Report clearly which stage passed: model update, simulation, code generation, toolchain build, binary generation, or only portable fallback.2930## MATLAB Coder Pattern3132Use this pattern before larger deployment:3334```matlab35cfg = coder.config("mex");36codegen -config cfg myFunction -args {coder.typeof(0,[100 1],[1 0])}37gold = myFunction(x);38actual = myFunction_mex(x);39assert(norm(gold-actual) < 1e-9)40```4142## Embedded Readiness4344Check:4546- Fixed-size vs variable-size arrays.47- Dynamic allocation and recursion.48- Unsupported functions.49- Numeric overflow and fixed-point scaling.50- Stack and heap impact.51- Timing budget and hardware target assumptions.5253## Simulink Codegen5455For models:5657- Update/compile the model after editing and before `slbuild`.58- Build only after simulation passes.59- Set solver and sample times explicitly.60- Run Model Advisor or relevant checks when available.61- Compare simulation outputs before and after code generation.6263## STM32 Embedded Coder Readiness6465For STM32 targets, do not treat "an executable exists on disk" as enough. Verify the full MATLAB-to-ST chain before blaming the model:6667- MATLAB products: Simulink, Simulink Coder, Embedded Coder, STM32 support package.68- MATLAB registration: `stm32cube.hwsetup.stm32Tools.getInstalledSTM32CubeMX()` returns the CubeMX directory MATLAB will use.69- ST tools: GNU Tools for STM32, CMSIS, CMSIS-DSP, STM32CubeMX, and the matching STM32Cube firmware package such as `STM32Cube_FW_F1_*`.70- Model binding: the model has a real `.ioc` path in `STM32CubeMX.ProjectFile`.71- Target hardware: `codertarget.targethardware.getRegisteredTargetHardwareNames` includes the required STM32 family, for example `STM32F1xx Based`.7273Prefer target data APIs over editing opaque structures:7475```matlab76tools = stm32cube.hwsetup.stm32Tools();77tools.updateSTM32CubeMXPath("C:\Users\me\AppData\Local\Programs\STM32CubeMX");7879codertarget.data.setParameterValue(model, "STM32CubeMX.ProjectFile", iocPath);80codertarget.data.setParameterValue(model, "STM32CubeMX.DeviceId", "STM32F103RBTx");81codertarget.data.setParameterValue(model, "STM32CubeMX.Family", "STM32F1");82```8384If `slbuild` hangs around "Generating code from STM32CubeMX project" or "Starting compilation process":8586- Check `STM32CubeMX.log`, generated `hardware` scripts, and background `java.exe` / `STM32CubeMX.exe` processes.87- Compare direct `STM32CubeMX.exe -q script` with MATLAB's `java -jar STM32CubeMX.exe -q script`; some CubeMX versions can behave differently.88- Prefer the MathWorks-recommended CubeMX version shown by `message("stm32:setup:CubeMXReqVersion").getString`.89- If non-ASCII paths are involved, copy the model and `.ioc` to an ASCII scratch path and retry once to isolate path encoding from model errors.90- Only use a local CubeMX wrapper as a documented workaround after recording the direct failure and the real CubeMX path.9192## Output9394Report target, generated artifact path, report path, golden comparison result, and limitations.