Simulink coder: blocks value mapping into memory address

2 次查看(过去 30 天)
Dear support,
I'm using Simulink coder to generate an executable for Windows from a simple Simulink model.
Once the .exe is built I would like to change the value of some Simulink blocks, i.e tunable parameter, constans, gain.
Is there a way to get from the Simulink coder a memory mapping of the Simulink blocks, so that I can retrive the memory address of the block and replace its value?
Thank you in advance for your reply!

回答(1 个)

Harsh
Harsh 2025-3-28
To change Simulink parameters when running an executable without needing to recompile, you can modify the generated code to read data from an external file within the model's step function. Here is how you can achieve this:
  • Create a Tunable Parameter: Define a "Simulink.Parameter" object and set its storage class to "ExportedGlobal". This ensures that the parameter appears as a global variable in the generated C code. Below is an example code snippet to create a tunable parameter "myGain" for "Gain" block -
myGain = Simulink.Parameter(2); % Initial gain set to 2
myGain.CoderInfo.StorageClass = 'ExportedGlobal';
set_param('TestModel/Gain', 'Gain', 'myGain');
  • Modify the Generated Code to load parameters from an external file: After generating the code from your Simulink model, update the "step" function in the generated "YourModel.c" file. Modify this function to read parameter values from an external source, such as a file. This approach allows you to dynamically update the parameter values without needing to recompile the executable.
I hope this resolves your query. Happy coding!

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by