set parameters read from outside a compiled simulink model at startup/initialization

4 次查看(过去 30 天)
Hi,
I have a Simulink Model I wish to compile into an executable which works fine except that I need to be able to set some parameters/variables at the start of the program or in the initialization phase.
So far, I have no hand-written code (.c or .h) files where I could add some lines.
What do I have to do and where would I place the code?
As of now, I've been using the following line to compile the simulink model:
mcc('-N','-p','icomm', '-m','Reglerexe','-a',['*',ext],'-a',['*',ext_thunk{:}], '-d', myDir)
Thanks in advance for your help!
Swantje

采纳的回答

Jack
Jack 2025-3-8

When compiling a Simulink model into an executable, there isn’t a built-in mechanism to automatically read external parameters at startup. One common solution is to add code in one of the model callbacks (for example, the PreLoadFcn or InitFcn) that reads parameters from an external source (such as a MAT file or environment variables) and then assigns them to the model workspace using set_param. Alternatively, you can create a custom main function that accepts command‐line arguments, uses those arguments to set parameters via set_param (or by writing to the model workspace), and then starts the simulation. This custom main function can be compiled along with your model using mcc. Both approaches allow you to set parameters at initialization without manually modifying generated C or header files.

Follow me so you can message me anytime with future questions. If this helps, please accept the answer and upvote it as well.

  3 个评论
Swantje Dettmann
Swantje Dettmann 2025-3-10
Thanks a lot for your answer at lightnig spped and on the weekend!
It clarified quite a bit and showed different paths. In the end I managed to read values into the model even easier using Parameters.Value.MyVar = Value read from the OPC Server, once I had figuered out the required settings in the model.
Nevertheless, thanks again!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Event Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by