Simulink external mode running time pause while processing a script

4 views (last 30 days)
I am relatively inexperience with matlab. I wanted to made an app from app designer and currently trying to simulate real time input data from a file into the app. I am currently using Matlab 2021b and was largerly successful in doing this by using simulink to read and loop the data file and then feed it into the app through event listener. However, the app does contain matlab functions that takes a while to process. The issue that i noticed was that the run time in simulink stop when those matlab functions within the app is called and being process. This means that the input from simulink into the app stops when the functions is process. I was wondering what could possibly cause this to occur and is there a way around it (ie. have the data continuously flow even when the functions are being process). I know that the functions takes a very long time to process and it would miss certain data input while processing but i want it to be intentional. I have also include the simulink model that i used and also the main processing Matlab function if that is of any help. Thanks in advance
Below is majority of the app code

Answers (1)

Yatharth
Yatharth on 22 Feb 2024
Hi,
It seems that the MATLAB functions within your app are causing the Simulink simulation to halt during their execution. This happens because MATLAB operates on a single-threaded execution model, meaning that when a MATLAB function is running, it monopolizes the thread and other operations, including those in Simulink, are put on hold until the function is finished.
To address this, you can use asynchronous execution methods, such as the parfeval function from the Parallel Computing Toolbox, which allows MATLAB functions to run in the background without interrupting the main thread. You can find more information on parfeval in the MATLAB documentation here: https://www.mathworks.com/help/matlab/ref/parfeval.html
It's important to note that when using asynchronous execution, whether through threads or separate processes, there are certain restrictions. For example, you cannot manipulate the display, use global variables, or modify variables in the main process directly. Instead, the main process must collect the results once the computation is complete and then integrate them back into the relevant variables.
For further understanding of how to set up your model for multitasking execution, you can refer to additional resources and documentation provided by MathWorks. https://www.mathworks.com/help/rtw/ug/modeling-for-multitasking-execution.html
I hope this helps!

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!