Random lags while running app
조회 수: 31 (최근 30일)
이전 댓글 표시
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
dpb
2025년 7월 23일
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?
채택된 답변
Gavin
2025년 8월 3일
댓글 수: 1
dpb
2025년 8월 4일
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.
추가 답변 (1개)
Image Analyst
2025년 7월 24일
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
dpb
2025년 7월 24일
편집: dpb
2025년 7월 24일
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...
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!