Speed-up the process of plotting real-time data (GUI)

Hi.
I have an Arduino board sending analog values (accelerometer data) via Bluetooth to MATLAB and I need to plot the data in real-time in a GUI axes. The problem is that there's a huge delay when plotting the data (although doing it outside the GUI doesn't raise this problems)...
I know I should preallocate to avoid copying the data from one array to another inside the loop but I'm not sure how to do that in this situation.
The goal is to plot 3 different variables in 3 different axes (one for each variable). The code inside the M-file OpeningFcn function for the Figure file is shown below:
t = 1 ;
x = 0 ;
y = 0;
z=0;
interv = 10000 ; % considering 1000 samples
fwrite(dev,uint8(20));
values = zeros(interv,2);
for t=1:1:interv
ax=fscanf(dev,'%d',1);
ay=fscanf(dev,'%d',1);
az=fscanf(dev,'%d',1);
%values(t,:)=[a,b];
x = [ x, ax ];
axes(handles.axes1);
plot(x, 'r') ;
ylim([-200 200]);
drawnow;
end
fclose(dev);
Any help would be much appreciated.

 Accepted Answer

Sara - every time you plot some data, you are creating a new graphics object. In your case, you will be creating 10000 graphics objects...which may lead to performance degradation.
An alternative is to plot once and re-use that object for future updates. For example,
h=plot(NaN,NaN,'r');
for t=1:1:interv
ax=fscanf(dev,'%d',1);
ay=fscanf(dev,'%d',1);
az=fscanf(dev,'%d',1);
%values(t,:)=[a,b];
x = [ x, ax ];
axes(handles.axes1);
set(h, 'XData', x);
ylim([-200 200]);
drawnow;
end
You could also allocate your x array outside of your loop as
x = zeros(1,interv);
h=plot(NaN,NaN,'r');
for t=1:1:interv
ax=fscanf(dev,'%d',1);
ay=fscanf(dev,'%d',1);
az=fscanf(dev,'%d',1);
%values(t,:)=[a,b];
x(t) = ax;
axes(handles.axes1);
set(h, 'XData', x(1:t));
ylim([-200 200]);
drawnow;
end
Note how we only use x(1:t) when updating the plot graphics object.
I don't know if the above will help, but try it out and see!

5 Comments

Thank you Geoff,
Unfortunately both alternatives lauch a warning/error and the plotting doesn't happen.
Warning: Error creating or updating Line
Error in value of one or more of the following properties: XData YData
Array is wrong shape or size
> In defaulterrorcallback (line 12)
In realtime>realtime_OpeningFcn (line 49)
In gui_mainfcn (line 220)
In realtime (line 16)
Line 49 is the where the "drawnow" command is located.
Do you know how to solve this?
Best regards, Sara
Hi Sara - try changing the shape of x
x = zeros(interv,1);
from a row to a column array.
Even faster: Omit
axes(handles.axes1);
and set the YLim before the loop.
hmmm...you may have to update the YData instead of changing the x (as above). I should have tested the above before posting...
x = zeros(1, interv);
h=plot(NaN,NaN,'r');
for t=1:1:interv
ax=fscanf(dev,'%d',1);
ay=fscanf(dev,'%d',1);
az=fscanf(dev,'%d',1);
x(t) = ax;
axes(handles.axes1);
set(h, 'XData', 1:t, 'YData', x(1:t));
ylim([-200 200]);
drawnow;
end
Note how we need to update both the x and y data to keep them the same size.
This solution could increase the speed about 3 times but there is still a fair amount of delay about 2 to 3 seconds.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance 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!