Simulink Desktop Real-Time & MATLAB App Designer UI (App update lag)
13 views (last 30 days)
Show older comments
Hello All
I am working on building a UI using MATLAB App Designer and I am pulling data from a Desktop Real-Time model. The model is quite simple so there are no issues with missing ticks or any errors when I run it using Connected IO mode. I'm running the model at a fixed-step of 0.1 seconds. In the model properties I've set up an event listener as per another forum post that I've come across realting to this subject:
run ReadSerial.m; % Creates serial port objects used to get data from COM ports in the updateApp function
run BufferFlush.m; % Flushes the serial port buffers
% Block Paths
blk1 = 'ST2_Model/Constant1';
% Event
event = 'PostOutputs';
% Listener - run the update app script
listener = @updateApp;
h1 = add_exec_event_listener(blk1,event,listener);
The updateApp function is being used to update the app I've built, by first reading serial data from 4 differnt COM ports on my PC and a few parameters from simulink (i.e. current simulation time, and a few block outputs). It updates numeric and text fields on the app using the set() command. Ex: set(data.hh,'Value',height);
This method of using event listeners to trigger MATLAB to run an update function at regular intervals works, I know becuase I've done this before for another app and with Simulink Desktop Real-Time, however, when I run the app (even for short periods, say 30 seconds), it fails almost immedietly. What I mean by "it fails", is that the fields on the app either do not update period (even though the function is being called), or they update for the first 100 or so function calls and then stop updating until the simulation ends. I've timed the execution time of the update function using the tic/toc functions in matlab and it take approximately 0.0185 seconds to execute. This seems reasonable since it is being called every 0.1 seconds from the SLDRT model.
I should also mention that within the update function, there is a portion of code that is saving data to a table in the workspace. Data from the table is not being used to update the fields on the app, it is just saving the data so a variable SOUNDER_DATA. In a 30 second simulation the table is appended 300 times (running at 0.1 seconds remember) so the growing table could also be affecting the functions ability to execute within the alloted window:
if exist("SOUNDER_DATA","var") == 1 %Check if the table exists
timet = datetime('now','Format','default'); %get current time
newSounder = table(timet,SOUNDER,'VariableNames',{'Time','Chosen Sounder String'}); %build a table entry
SOUNDER_DATA = [SOUNDER_DATA;newSounder]; %append table entry to the current table
else
if exist("SOUNDER",'var') == 0
SOUNDER = ""; % Create empty string if the variable and table dont exist
end
timet = datetime('now','Format','default');
SOUNDER_DATA = table(timet,SOUNDER,'VariableNames',{'Time','Chosen Sounder String'}); %create the SOUNDER_DATA table
end
I am trying to find a reason as to why the app may be lagging behind / not updating. Any suggestions would be appreciated. I am quite confused becuase the model never "misses ticks" or has trouble executing, which suggests to me I am doing something wrong in my updateApp function that is causing the app to lag.
I've seen suggestions on some other MATLAB forums such as:
- The update function is being called too frequently and not executing properly
- Growing buffers / tables causing the funciton execution time to inrease as function calls increase
- You could try running the model and the GUI on separate threads / in parallel using a toolbox (I'm not sure how this one works)
Thanks in advance for any suggestions.
0 Comments
Accepted Answer
Peter Perkins
on 5 Jun 2023
This
[SOUNDER_DATA;newSounder]
can take some time, it's an expensive operation. If possible, better to save all those one-row tables in a cell array, and then call vertcat(cellOfTables{:}) once. Dunno if that's your bottleneck though.
Also,
datetime('now','Format','default')
is kind of unnecessary, just
datetime
would do the same thing.
0 Comments
More Answers (0)
See Also
Categories
Find more on Target Computer Setup 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!