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

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Jack
Jack am 8 Mär. 2025

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 Kommentare
Swantje Dettmann
Swantje Dettmann am 10 Mär. 2025
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!
Jack
Jack am 10 Mär. 2025
Bearbeitet: Jack am 10 Mär. 2025
Great :) Glad I could help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Event Functions finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by