Simulink control from app

10 visualizzazioni (ultimi 30 giorni)
Anna
Anna il 28 Feb 2025
Commentato: Anna il 15 Mag 2025
I'd like to control the simulation of a Simulink model from an app.
I have a Simulink model (model.jpg) with Analog Output/Input blocks connected to NI myDAQ. The model works well: I can pause simulation, continue simulation and stop simulation.
I have an app (matlab_app.jpg) with the Simulink control buttons from the Simulink library in the App Designer:
  • simulation starts with Run button
  • if I pause simulation the continue icon does not appear and simulation gets stuck
  • if I stop simulation I get an error (error.jpg)
I have tried the Simulink control buttons with a simple Simulink model (sourse+gain) and the buttons work properly so the problem seems to arise with the Analog Output/Input blocks. Why does it happen? Any suggestion how to fix the problem?

Risposta accettata

Rahul
Rahul il 14 Mag 2025
Modificato: Rahul il 14 Mag 2025
Hi Anna,
I understand that you are trying to control the simulation of your Simulink model with 'NI myDAQ' blocks through a MATLAB App Designer interface and are facing issues with pausing and stopping the simulation correctly.
The issue with pausing and stopping the simulation is caused by the real-time communication between the 'NI myDAQ' blocks and Simulink. These blocks operate continuously during simulation, and when a 'Pause' command is issued, Simulink attempts to halt, but the analog I/O remains actively communicating with NI myDAQ' at its sample rate. This is why the simulation appears unresponsive when paused.
Similarly, the error during the Stop command occurs because the 'getSimulationOutput' function is called while the simulation status is still Running. 'NI myDAQ' operations need to safely terminate before the status is updated to 'Inactive'.
To address this issue, you can consider adopting the following workarounds in your application:
  1. Switching to External Mode in Simulink: In External Mode, the NI myDAQ communication is more flexible and allows proper pausing and stopping through Simulink's interface.
  2. Event Listener for Pause and Stop: You can add the following event listeners in your MATLAB App to appropriately pause/stop 'NI myDAQ' simulation, as shown below:
function pauseSimulation()
set_param('model_name', 'SimulationCommand', 'pause');
while ~strcmp(get_param('model_name', 'SimulationStatus'), 'paused')
pause(0.1); % Wait for status update
end
end
function stopSimulation()
set_param('model_name', 'SimulationCommand', 'stop');
while ~strcmp(get_param('model_name', 'SimulationStatus'), 'stopped')
pause(0.1); % Wait for status update
end
end
You can refer to the following documentation link to know more callbacks and usage of 'set_param' functions:
Hope this helps!
  1 Commento
Anna
Anna il 15 Mag 2025
Thanks for the exaplanation, now I understand the reason of the behaviour. I will test the workarounds you suggest!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by