필터 지우기
필터 지우기

Stop MATLAB/Simulink execution from app and regain control

조회 수: 22 (최근 30일)
Luka Gacnik
Luka Gacnik 2022년 8월 5일
답변: Sai Teja G 2023년 9월 6일
I'm building an app, using App Designer, where certain parameters are configured then ".m" file is called and configuration parameters passed to its function. This happens when I press "Start" button in App Designer, then the code in Editor and also Simulink sessions start executing.
There is code in MATLAB Editor, which starts a Simulink session (Editor pre-processing -> Simulink session -> Editor post-processing -> Repeat). After Simulink is done executing, control is returned to Editor, which updates some parameters and new Simulink session starts executing. This keeps repeating until certain conditions are met, then function from ".m" file terminates. After that I assume the control is returned to UI of App Designer, and user can (re)start the simulation.
Now, what are the options to stop Simulink and ".m" file from executing (by pressing certain pushbutton in the app) and returning control to the app? Since it seems Simulink execution cannot be terminated from command line, I would say I need to wait for Simulink session to finish. Then maybe (somehow) manually put a breakpoint in MATLAB Editor, right after "sim" command is executed. And then using some function to terminate the function in the ".m" file (called from the app).
What are the options in such situation? Ideally, after pressing "Stop" pushbutton, simulation would be terminated immediately and it could also be restarted by pressing "Start" pushbutton right away.

답변 (1개)

Sai Teja G
Sai Teja G 2023년 9월 6일
Hi Luka,
I understand that you want to stop and restart the simulation on button clicks in AppDesigner.
To pause and restart the simulation, you can utilize the 'load_system()' and 'set_param()' functions in the callback function of your AppDesigner button. Below is a sample code snippet that demonstrates this functionality. Please review it and adapt it according to your specific requirements.
properties (Access = public)
global_variable=0; % used as a variable to check either to stop or restart the simulatiion
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function start_simulation(app, event)
model='start';%start is a sample simulink model name
if app.global_variable==0%start the simulation for first time
load_system(model);%load system
Simout=sim(model);%start simulation
app.global_variable=1;
elseif app.global_variable==1%stopping the simulatiom
set_param(model, 'SimulationCommand', 'stop');%stopping the simulation
app.global_variable=2;
else %restarting the simulation
set_param(model, 'SimulationCommand', 'start');
app.global_variable=1;
end
end
end
You can also refer to the documentation below to gain a better understanding of the 'set_param()' function -
Hope this helps!

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by