Stop MATLAB/Simulink execution from app and regain control

6 visualizaciones (últimos 30 días)
Luka Gacnik
Luka Gacnik el 5 de Ag. de 2022
Respondida: Sai Teja G el 6 de Sept. de 2023
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.

Respuestas (1)

Sai Teja G
Sai Teja G el 6 de Sept. de 2023
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!

Categorías

Más información sobre Create Apps to Control Simulations en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by