MATLAB: Change parameters in Simscape model executable

code generationparametersSimscape

I converted a Simscape model to C code and Matlab generated a .exe file as well. The idea is to deploy this model without giving access to the model content. The end-user will use Matlab to run this executable.
The executable runs and produces correct results, however I cannot figure out how to change the parameters used for the simulation.
Attached is a basic example where I would like to be able to change the value of the AC Voltage Source peak amplitude, which is currently given by a workspace variable.

Best Answer

you can follow below steps to change parameters in Simscape model executable
  • In Preference -> Simscape enable "Show runtime parameter settings".
  • Ensure that you have a Simulink parameter that defines the specified Run-time parameter you want to change.
  • Set the parameter to Run-time.
  • Ensure the Simulink parameter's storage class is "Model Default".
  • Enable "Generate Example main program" in Simulink Configuration Parameters under Code Generation > Templates > Custom Templates.
  • Enable "Generate C API for Parameters" in the Configuration Parameters under Code Generation > Interface > Data Exchange Interface.
  • Generate code for the model.
You will have to edit the example main file to set the value for the parameter you want to change, but a function should be produced that looks like "rtDataAddrMap". Inside the main function before "rt_OneStep()" is called, handle the value for the tune-able parameter.