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

34 次查看(过去 30 天)
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.

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2023-4-14

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:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Deployment, Integration, and Supported Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by