Speed-up the process of plotting real-time data (GUI)
1 view (last 30 days)
Show older comments
Sara Beatriz Lobo
on 23 May 2018
Commented: Mohammad Amirkhani
on 16 May 2021
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.
0 Comments
Accepted Answer
Geoff Hayes
on 23 May 2018
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
Geoff Hayes
on 23 May 2018
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.
Mohammad Amirkhani
on 16 May 2021
This solution could increase the speed about 3 times but there is still a fair amount of delay about 2 to 3 seconds.
More Answers (0)
See Also
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!