How can I control aspects of my simulation in generated code?

1 view (last 30 days)
Hi,
I have a simulation in which I am planning to run in generated code. Is there any way that I can access the simulation through the executable, such as through a inserting Matlab code or an external function that would change a variable and update the simulation from that point?
For example, a constant block that takes a variable from the workspace, is there any way to change the value of the constant during the simulation in the generated executable?

Accepted Answer

Sebastian Castro
Sebastian Castro on 3 Aug 2015
By default, parameters in Simulink models are inlined so that the code is more efficient and uses less memory. However, you can certainly designate individual parameters as tunable so that you can access them from the main function driving your generated code. The most common way this is done is to place the tunable parameters in a globally visible structure.
The main link is here, which shows you lots of options to tackle this problem: http://www.mathworks.com/help/ecoder/tunable-parameters-and-expressions.html
- Sebastian
  4 Comments
Matthew C
Matthew C on 4 Aug 2015
Thank you very much! One last question, where can I find this main file? I have found the source c file where the step function is defined, but I was under the impression that the main file is a mathworks template that just gets compiled into the executable.
Sebastian Castro
Sebastian Castro on 4 Aug 2015
It is a MathWorks template, which gets generated with Embedded Coder ( ert.tlc ) if you go to the "Code Generation > Templates" options and have "Generate an example main program" selected. This is on by default.
Once you generate code, you will see a file named ert_main.c in the generated code folder ( modelName_ert_rtw ). This is your template which you can then modify. Notice that you get a modelName.bat file in that folder too. That is what you would run to recompile the executable using the new main source code.
>> !modelName.bat
Alternatively, you could just write your own main file and compile the whole thing elsewhere. Depends on whether or not you need to integrate the generated code into a larger code base.
- Sebastian

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder 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!