Simulink control from app

조회 수: 4 (최근 30일)
Anna
Anna 2025년 2월 28일
댓글: Anna 2025년 5월 15일
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?

채택된 답변

Rahul
Rahul 2025년 5월 14일
편집: Rahul 2025년 5월 14일
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
Anna
Anna 2025년 5월 15일
Thanks for the exaplanation, now I understand the reason of the behaviour. I will test the workarounds you suggest!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scopes and Data Logging에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by