How do I run my External Mode simulation from the command line or a MATLAB script?

37 views (last 30 days)
I would like to run my External Mode simulation from the command line or a MATLAB script. My aim is to monitor and parameterize an application running on targets such as Simulink Desktop Real-Time, Simulink Real-Time, or other hardware that has External Mode support enabled, without the need to press the "Run on Target" or "Monitor & Tune" button in Simulink Toolstrip.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 May 2023
Edited: MathWorks Support Team on 14 Apr 2023

Run External Mode simulation from the MATLAB Command Window:

To run these commands, you must have a Simulink model open and a target application running.
1. Set the model simulation mode to external mode.
>> set_param(gcs,'SimulationMode','external');
2. Connect Simulink to the target application.
>> set_param(gcs,'SimulationCommand','connect')
This command is equivalent to pressing the "Connect to target" button.
3. Run generated model code.
>> set_param(gcs,'SimulationCommand','start');
4. To tune a parameter, change its workspace variable value through a line command. For example, if a block parameter value is specified as a Simulink.Parameter object, assign the new value to the Value property.
>> myParamObj.Value = 5.23;
To download the new value to the target application, update the model.
>> set_param(gcs,'SimulationCommand','update');
5. Stop the target application and disconnect Simulink from the target environment.
>> set_param(gcs,'SimulationCommand','stop');
6. To disconnect Simulink from the target application without stopping the execution of generated code, use this command:
>> set_param(gcs,'SimulationCommand','disconnect');
.

Run External Mode simulation from a MATLAB script:

The set_param commands that use the 'SimulationCommand' argument are asynchronous. If you run the commands successively from a script, each command starts without waiting for the previous command to complete. To check that each command is complete, in the script, use the get_param command with the 'SimulationStatus' argument. 
For example, for steps 1 to 3, specify these commands in the script:
set_param(gcs,'SimulationMode','external');
set_param(gcs,'SimulationCommand','connect');
isExternalSimulationActive = false;
while ~isExternalSimulationActive
simStatus = get_param(gcs, 'SimulationStatus');
isExternalSimulationActive = strcmp(simStatus, 'external');
end
set_param(gcs,'SimulationCommand','start');
For more information, see the Simulink and Simulink Coder documentation:

More Answers (0)

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!