Plotting to multiple GUI axes simultaneously using a for loop

Hi,
I am trying to build a GUI to plot real-time data from an Arduino. The code needs to read the values in from four analog pins and plots the data on each of the 4 axes in the Matlab GUI, respectively. The code I have at the moment works, however it is rather slow. Can anyone provide a means to make this faster/remove inefficiencies or any other assistance would be helpful? The code is as follows:
x1=0;
x2=0;
x3=0;
x4=0;
for k=1:1:100
%plot 1
A1=readVoltage(a,'A1');
x1=[x1,A1];
plot(handles.axes1,x1,'LineWidth',2);
grid on;
drawnow;
%plot 2
A2=readVoltage(a,'A2');
x2=[x2,A2];
plot(handles.axes2,x2,'LineWidth',2);
grid on;
drawnow;
%plot 3
A3=readVoltage(a,'A3');
x3=[x3,A3];
plot(handles.axes3,x3,'LineWidth',2);
grid on;
drawnow;
%plot 4
A4=readVoltage(a,'A4');
x4=[x4,A4];
plot(handles.axes4,x4,'LineWidth',2);
grid on;
drawnow;
pause(0.01);
end

 Accepted Answer

x = zeros(4, 101); %Preallocation is much faster
Px = gObjects(1, 4);
for c = 1:4 %Initilize all your plots, for 4 pins
Px(c) = plot(handles.(['axes' num2str(c)]), 0, 'LineWidth', 2); %Store your plot handles
grid on;
end
for k = 2:101 %Did k to 101 since it looks like you want to keep the 1st point as a 0
for c = 1:4
x(c, k) = readVoltage(a, ['A' num2str(c)]);
set(Px(c), 'XData', 1:k, 'YData', x(c, 1:k)); %Setting the XData and YData directly to prevent redrawing plot from scrap.
end
drawnow; %Do only 1 drawnow, instead of 4 drawnow you had before
end
Note that instead of 4 plots, you could just have 1 plot with 4 lines for a little extra speed and simplicity in GUI.

4 Comments

Hi, Thank you for the response. Just the code I was looking for. I can logically understand what you have written and theoretically, it should work. However, I run into an issue with:
Px(c) = plot(handles.(char('axes + c)), [], 'LineWidth', 2)
and get the error: "Reference to non-existent field 'byft'" which is clearly the result of the character conversion of ('axes' + c).
Even if I force the the value the be 'axes1' for example, such that you get handles.axes1 in theory, it still says "Reference to non-existent.." The tag of the axes is axes1.
I'm using Matlab 2015a if that makes any difference. Sorry If my reply is silly, have very little experience in Matlab and even less with its "handles" system.
I greatly appreciate your help.
I see, matlab 2015a doesn't have string capabilities yet. In that case, you'll have to change things to
Px(c) = plot(handles.(['axes' num2str(c)]), [], 'LineWidth', 2)
and
x(c, k) = readVoltage(a, ['A' num2str(c)]);
I edited the answer above
Wonderful! Everything working now. I did however changer the second argument of plot function from a [] to a 0 in Px(c) = plot(....) as it gave "In an assignment A(:) = B, the number of elements in A and B must be the same."
But everything is working wonderfully. So much smoother.
Thank you so much for your help!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 9 Jul 2018

Commented:

on 10 Jul 2018

Community Treasure Hunt

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

Start Hunting!