Random lags while running app
25 views (last 30 days)
Show older comments
I have an App Designed application that collects data from an Arduino Pico.
The Pico runs individual trials with accurate to submillisecond timing. MATLAB starts each trial as soon as the previous one is complete. Data is saved in tables as I found that writing to disk after each trial took too long. Even so, the time between trials is longer than it should be AND it can be extremely long on occasion. I've plotted each trial duration and the timing of the end of one to the start of the next. Any ideas as to why MATLAB occasionally takes long breaks? I've tried clearing variables between each trial (doesn't seem to affect my app tables). Removed calls to get time of day (useing Pico Timer for data) various other attemps with Profiler but no luck since the error is rare. Copilot wasn't much help either.
I tried updating to version R2025a and that was much worse!

Any help much appreciated. Code for the app is here, it's big:
https://drive.google.com/file/d/1mwCtRJ-l_2fhk26JYzz2IxidDqkU3IV1/view?usp=sharing
16 Comments
dpb
on 23 Jul 2025
Good spelunking to have uncovered the "where"...can you tell whether it is a delay from the time the MATLAB line is encountered before the transmission or a delay in actually executing the code itself?
I'm not sure there is any way you can control that, at least with MATLAB-level code; you're pretty-much at the mercy of the OS there, I think.
Only a couple of ideas to try otomh; first would be to use the low-level char() array for the data instead of the string class; this is just the string of bytes without the overhead of the string wrapper to be dereferenced on sending.
str='G';
if ~isempty(str)
write(app.PicoCom, str, 'char');
I don't know that if you dropped into mex and C to do the communications you could ensure better performance or not; the internals are all builtin functions already, so it would only be whether that could prevent some sort of delay in the MATLAB code itself between lines of code being dispatched.
What would it take to just build a command line interface that does nothing but ping the Arduino without any other distractions and see if that behaves more reliably?
Accepted Answer
Gavin
on 3 Aug 2025 at 20:53
1 Comment
dpb
on 4 Aug 2025 at 17:53
Ah, so! Glad you were able to resolve it...
"...it takes a LONG time to update a graph"
Are you redrawing or updating the data in place? For speed, unless it is mandtory for other things to be updated, just modifying 'X|YData' is much faster, although if rendering itself is an issue, that won't be helped.
More Answers (1)
Image Analyst
on 24 Jul 2025
See if the problem goes away if you set the priority of any of your program's processes to High. Type Control-Shift-Esc to bring up Task manager. On the "Details" panel (Window 11, slightly different in Windows 10) find your processes (any MATLAB processes, your executable, and anything else related to your running of your program. Then right click and go to "Set priority" and set it to High. See if that avoids the problem.

1 Comment
dpb
on 24 Jul 2025
Edited: dpb
on 24 Jul 2025
Raising priorities was discussed early on, IA, I do not know whether Gavin actually experimented with doing so or not, however...
It would still be interesting to see if could somehow tie in what is happening with OS context switching at the same time these delays occur...
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware 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!