Simulink Scope "Open at Simulation Start"

63 views (last 30 days)
I have several scopes in my simulink model that I run from a GUI. I would like to provide users the option to open one or several of those scopes when the simulation runs. I know there's a checkbox on the scope menu for "Open at Simulation Start". Is there a way I can interact with that property from Matlab at the time I run the simulink model? I can create variables in the workspace based on the GUI checkbox status, but how can I feed that into the scope in the model?

Accepted Answer

Pratik
Pratik on 30 Nov 2023
Yes, you can interact with the "Open at Simulation Start" property of a Scope block in Simulink using MATLAB. You can use the `set_param` function to modify the properties of the Scope block at the time you run the Simulink model.
Here's an example of how you can achieve this:
% Assuming you have a variable named 'openScope' in your workspace based on the GUI checkbox status
% 'openScope' should be a logical value indicating whether the scope should be opened at simulation start
% Set the name of the scope block in your Simulink model
scopeBlockName = 'YourScopeBlockName'; % Replace with the actual name of your scope block
% Set the 'Open at Simulation Start' property based on the 'openScope' variable
set_param([gcs '/' scopeBlockName], 'OpenAtSimulationStart', num2str(openScope));
Replace `'YourScopeBlockName'` with the actual name of your Scope block. This code will set the "Open at Simulation Start" property of the specified Scope block based on the value of the `openScope` variable in your MATLAB workspace.
  4 Comments
Fangjun Jiang
Fangjun Jiang on 30 Nov 2023
Good to know! I thought I saw somewhere that 'on'/'off' can be replaced by 1/0.
But in R2022b, num2str() still can't be applied. That is my point.
>> set_param(gcb,'OpenAtSimulationStart','off')
>> set_param(gcb,'OpenAtSimulationStart','on')
>> set_param(gcb,'OpenAtSimulationStart',1)
>> set_param(gcb,'OpenAtSimulationStart',0)
>> set_param(gcb,'OpenAtSimulationStart',true)
Invalid setting in Scope block 'Scope6' for parameter 'OpenAtSimulationStart'
Caused by:
Option specified is not valid.
>> set_param(gcb,'OpenAtSimulationStart',false)
Invalid setting in Scope block 'Scope6' for parameter 'OpenAtSimulationStart'
Caused by:
Option specified is not valid.
>> set_param(gcb,'OpenAtSimulationStart',num2str(1))
Invalid setting in Scope block 'Scope6' for parameter 'OpenAtSimulationStart'
Caused by:
Option specified is not valid.
>> set_param(gcb,'OpenAtSimulationStart',num2str(0))
Invalid setting in Scope block 'Scope6' for parameter 'OpenAtSimulationStart'
Caused by:
Option specified is not valid.
>> set_param(gcb,'OpenAtSimulationStart',num2str(true))
Invalid setting in Scope block 'Scope6' for parameter 'OpenAtSimulationStart'
Caused by:
Option specified is not valid.
Jeffrey Topp
Jeffrey Topp on 1 Dec 2023
So I am able to execute this from the command line, but this requires the Simulink simulation be open. When I'm running from my Gui I don't require that. It should open up the simulation and the appropriate scopes.
So I tried to create a function block inside the simulation to execute the set_param commands. Simulink is telling me this function is "not supported for code generation".

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!