Alternative to "drawnow" to plot real time data inside while loop

I'm reading data from ADC in real time (while loop) and I'm trying to plot in real time. Now, I can't use
pause on; hold on; etc
Because it interferes with ADC's API and how it samples the data. My code looks something like that and it does plot in real time.
while true %sampling from ADC
%read data from ADC and update
a = datafromADC;
plot3(t, real(datafromADC), imag(datafromADC))
drawnow;
end
It works but I notice that when I increase sampling rate above certain value, ADC's API crashes, giving me an error pointing to .NET library that stopped working. When I increase sampling rate but comment plot3 and drawnow,
while true %sampling from ADC
%read data from ADC and update
a = datafromADC;
%plot3(t, real(datafromADC), imag(datafromADC))
%drawnow;
end
This code works and I can plot it later but without ability to plot in real time. My theory is that ADC's API and "drawnow" compete for resources and cause API to crash.
Are there alternatives to "drawnow" to plot complex data in real time? Right now my only option is to sample data and store it, terminate "while" loop and run a different script to display it.

댓글 수: 3

"This code works and I can plot it later but without ability to plot in real time. My theory is that ADC's API and "drawnow" compete for resources and cause API to crash. "
My theory is the API is buggy.
Paul Pogba's answer moved here as a comment.
I think the problem is that I'm plotting too fast. I'll try to change the code and comment here if it helped.
Bruno Luong
Bruno Luong 2019년 9월 12일
편집: Bruno Luong 2019년 9월 12일
You can't plot to fast believe me, using PLOT3 and DRAWNOW.
To plot faster you must use SET(...) + DRAWNOW.
But I bet you can still update your plot faster than 100 Hz, which is slow for a computer.

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

 채택된 답변

Adam Danz
Adam Danz 2019년 9월 12일
편집: Adam Danz 2019년 9월 13일
"Are there alternatives to "drawnow" to plot complex data in real time?"
I can think of two things to try.
1) instead of calling plot3() and dealing with all of its overhead on each iteration, just plot it once and update the X/Y/ZData properties.
2) instead of executing drawnow on each iteration, use a timer to update the plot every so often.
Here are those suggestions quasi -implemented; you can fit it to your needs.
% set up drawnow timer
t = timer;
t.StartDelay = 1;
t.TimerFcn = @(~,~)drawnow;
t.Period = 1; %update every 1 sec.
h = plot3(nan,nan,nan); % Plot empty 3D line
start(t) % start drawnow timer
while true %sampling from ADC
%read data from ADC and update
a = datafromADC;
h.XData(end+1) = t; % ?
h.YData(end+1) = real(datafromADC);
h.ZData(end+1) = imag(datafromADC);
end
stop(t) %stop timer
*Not tested
[addendum]
3) before plotting this figure, clear all other figures. Every time drawnow() is executed it will update all graphics which could easily cause a bottleneck.

댓글 수: 4

Also remember drawnow limit
That would be even better than a timer. Paul Pogba, try that first. If you still have problems, try the timer in my answer. If you still have problems after that, you need to dig deep and determine if it's an api issue or not.
h = plot3(nan,nan,nan); % Plot empty 3D line
while true %sampling from ADC
%read data from ADC and update
a = datafromADC;
h.XData(end+1) = t; % ?
h.YData(end+1) = real(datafromADC);
h.ZData(end+1) = imag(datafromADC);
drawnow limitrate
end
Paul Pogba
Paul Pogba 2019년 9월 12일
편집: Paul Pogba 2019년 9월 12일
What I have right now is kinda working. Before I would plot every time I capture data and it appears to be too fast. Now I plot once every 10 times I capture data and it works OK. However, I noticed that when I point my mouse to the points on the graph, it freezez a little and API crashes so I still believe that plotting 40000 points in one graph and sampling at the same times causes the problem.
Adam Danz
Adam Danz 2019년 9월 13일
편집: Adam Danz 2019년 9월 13일
If you're still calling the plot3() function every time you want to update the figure rather than updating the X/Y/ZData, there's still too much overhead (see how that's done in my answer).
If you think the number of points on the graph is slowing things down, you could plot every x samples or, better yet, the mean of every x samples.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

질문:

2019년 9월 12일

편집:

2019년 9월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by