0

I have a simple Simulink model that passes a constant variable to the workspace. enter image description here

I've converted the Simulink model to an exe-file using Simulink Coder (with grt.tlc as the target system file). However, I'm struggling to change the value of the constant myConstantValue and send it to the executable file. I don't want to compile every time when I want to change the value.

I've tried several options, such as saving the new myConstantValue in a .mat file and calling it in MATLAB with system('MyModel.exe -p myConstantValue.mat'), or writing myConstantValue=100 to a .txt file and calling it with system('MyModel.exe -i input_params.txt'). Unfortunately, none of these methods work—the executable always uses the initial data.

I've also tried changing various settings in the Code Generation settings of Simulink, such as setting the default parameter behavior to "Tunable", but this hasn't made any difference.

Could you please help me figure out how to change the constant value and pass it to the executable file?

1 Answer 1

0

Just putting the value of the constant block as a parameter does not expose it to external access since when the code is generated it will be buried somewhere with local scope accessibility.

You need to use the Simulink.Parameter object. To do so keep the model as it is and create in the workspace a the object with the same name as the variable you are passing to the constant:

myCoinstantValue= Simulink.Parameter(); 
myCoinstantValue.CoderInfo.StorageClass = 'ExportedGlobal';

The ExportedGlobal property is the one that exposes the parameter to external access by making the parameter a global variable in the generated code.

There is other properties you can set as default value, data type, unit, ...

You should now be able to pass it either using the set_param or using system commands as you would with any other .exe

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Ferror, thank you for your answer. But it is still not working. The command set_param cannot call the exe-file, and if I use the command system('test.exe -p constantValue.mat') % I save the new value of myConstantValue inside the mat-file. I still only get the initial value that has been used to compile the Simulink model.
I asked ChatGPT, it told me "When you compile a Simulink model into a standalone executable (.exe) using Simulink Coder, the generated code is fixed at compilation time. The executable runs independently of MATLAB and does not support runtime parameter changes through MATLAB commands" Do you have other opinions or ideas to solve this problem? Thank you!
I understand, set_param is not used ot call executable but rather interact with the model parameters. Did you use the simulink parameter as i indicated? If so you cant pass a file, you need to pass a string with the parameter name and value as if it was a matlab command window command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.