How can I force variables that are optimized away to appear in the generated code using Coder products?

11 visualizaciones (últimos 30 días)

I have a model in Simulink for which I would like to generate code. In the generated code, I find that some of the variables are optimized away by the coder. These variables are outputs of a lower-level subsystem and are not used by the higher-level subsystem, but they contain information that is crucial for debugging.
How can I force these variables to appear in the generated code?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 1 de Jul. de 2025
There are a few methods you may consider:
1) Mark the signal as a Test Point
If the name of the signal does not appear in the generated code, try marking it as a Test Point by following these steps:
  1. Open Signal Properties:
    • Right-click on the signal line in your Simulink model.
    • Select "Properties" from the context menu.
  2. Enable Test Point:
    • In the Signal Properties dialog, go to the "Logging and accessibility" section.
    • Check the Test Point option.
This approach is particularly useful when you want to retain intermediate signals for debugging, testing, or code generation purposes. 
2) Name the signal
Try naming the signal by double-clicking the signal line in your Simulink model.
3) Use "Outport" blocks instead of "Terminator" blocks
If the outputs of the lower-level subsystem are terminated by a "Terminator" block, replace that with an "Outport" block. This forces the upper-level subsystem to "use" the signal, so the signal will appear in the generated code. Note that this will alter the structure of the model, especially for large models.
4) Change the Sampling Time property of "Constant" blocks
If you have "Constant" blocks, make sure the sampling time is "-1" instead of "Inf". The coder may treat the signal as a block constant if the Sampling Time is set to "Inf".

Más respuestas (0)

Categorías

Más información sobre Configure Simulation Conditions en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by