Two graphs at the same time

3 views (last 30 days)
Ameen Mouazzen
Ameen Mouazzen on 18 Apr 2019
Commented: Ameen Mouazzen on 19 Apr 2019
PLEASE HELP
Hello,
I have this code that is used with arduino package
the code will be used to get data from two sensors:
  1. load cell that measures load
  2. displacement sensor that measures displacement
problems:
  1. this code is showing two graphs but only one graph has labels
  2. only one graph is showing data results
this is the code: (please note that i'm new to MATLAB and coding in general)
clear
clc
a=arduino('com3','Uno')
loadcell=addon(a,'ExampleAddon/HX711',{'D2','D3'})
plotTitle1 = 'Load VS Time';
xLabel1 = 'Elapsed Time (s)';
yLabel1 = 'Load (KN)';
legend1 = 'Load Cell 1'
plotTitle2 = 'Displacement VS Time';
xLabel2 = 'Elapsed Time (s)';
yLabel2 = 'Displacement (mm)';
legend2 = 'Displacement Sensor 1'
yMax1 = 10000000
yMin1 = 0
yMax2 = 10000000
yMin2 = 0
plotGrid = 'on';
min1 = 0;
max1 = 10000;
min2 = 0;
max2 = 10000;
delay = .001;
time = 0;
data1 = 0;
data11 = 0;
data12 = 0;
data2 = 0;
data21 = 0;
data22 = 0;
count = 0;
subplot(2,4,1)
plotGraph1 = plot(time,data1,'-r')
subplot(2,4,2)
plotGraph2 = plot(time,data2,'-r')
hold on
title(plotTitle1,'FontSize',5);
xlabel(xLabel1,'FontSize',5);
ylabel(yLabel1,'FontSize',5);
axis([yMin1 yMax1 min1 max1]);
title(plotTitle2,'FontSize',5);
xlabel(xLabel2,'FontSize',5);
ylabel(yLabel2,'FontSize',5);
axis([yMin2 yMax2 min2 max2]);
grid(plotGrid);
tic
while ishandle(plotGraph1)
dat1 = read_HX711(loadcell)-1940.225269
count = count + 1;
time(count) = toc;
data1(count) = dat1(1);
set(plotGraph1,'XData',time,'YData',data1);
axis([0 time(count) min1 max1]);
pause(delay);
end
hlod on
while ishandle(plotGraph2)
dat2 = readVoltage(a,'A0')*80
count = count + 1;
time(count) = toc;
data2(count) = dat2(1);
set(plotGraph2,'XData',time,'YData',data2);
axis([0 time(count) min2 max2]);
pause(delay);
end
delete(a);
disp('Plot Closed and arduino object has been deleted');
hold on
  2 Comments
Hans123
Hans123 on 18 Apr 2019
% Would fix problem 1
clear
clc
a=arduino('com3','Uno')
loadcell=addon(a,'ExampleAddon/HX711',{'D2','D3'})
plotTitle1 = 'Load VS Time';
xLabel1 = 'Elapsed Time (s)';
yLabel1 = 'Load (KN)';
legend1 = 'Load Cell 1'
plotTitle2 = 'Displacement VS Time';
xLabel2 = 'Elapsed Time (s)';
yLabel2 = 'Displacement (mm)';
legend2 = 'Displacement Sensor 1'
yMax1 = 10000000
yMin1 = 0
yMax2 = 10000000
yMin2 = 0
plotGrid = 'on';
min1 = 0;
max1 = 10000;
min2 = 0;
max2 = 10000;
delay = .001;
time = 0;
data1 = 0;
data11 = 0;
data12 = 0;
data2 = 0;
data21 = 0;
data22 = 0;
count = 0;
subplot(2,4,1)
plotGraph1 = plot(time,data1,'-r')
title(plotTitle1,'FontSize',5);
xlabel(xLabel1,'FontSize',5);
ylabel(yLabel1,'FontSize',5);
axis([yMin1 yMax1 min1 max1]);
subplot(2,4,2)
plotGraph2 = plot(time,data2,'-r')
hold on
title(plotTitle2,'FontSize',5);
xlabel(xLabel2,'FontSize',5);
ylabel(yLabel2,'FontSize',5);
axis([yMin2 yMax2 min2 max2]);
grid(plotGrid);
tic
while ishandle(plotGraph1)
dat1 = read_HX711(loadcell)-1940.225269
count = count + 1;
time(count) = toc;
data1(count) = dat1(1);
set(plotGraph1,'XData',time,'YData',data1);
axis([0 time(count) min1 max1]);
pause(delay);
end
hlod on
while ishandle(plotGraph2)
dat2 = readVoltage(a,'A0')*80
count = count + 1;
time(count) = toc;
data2(count) = dat2(1);
set(plotGraph2,'XData',time,'YData',data2);
axis([0 time(count) min2 max2]);
pause(delay);
end
delete(a);
disp('Plot Closed and arduino object has been deleted');
hold on
Ameen Mouazzen
Ameen Mouazzen on 18 Apr 2019
Edited: Ameen Mouazzen on 18 Apr 2019
thank you very much for your help.
but this change made all graphs show no results at all

Sign in to comment.

Answers (1)

Yashika
Yashika on 19 Apr 2019
use 'figure()' when you need further plots in different figure.
for eg,
figure(1)
plot(x1,y1)
hold on;
figure(2)
plot(x2,y2)
hold off
and the remaing thing like axis labels, legends you can write before hold on/off.
  1 Comment
Ameen Mouazzen
Ameen Mouazzen on 19 Apr 2019
thank you very much.
I tried this but didn't change anything.
I'm sure I did it wrong.
can you please check?
clear
clc
a=arduino('com3','Uno')
loadcell=addon(a,'ExampleAddon/HX711',{'D2','D3'})
plotTitle1 = 'Load VS Time';
xLabel1 = 'Elapsed Time (s)';
yLabel1 = 'Load (KN)';
legend1 = 'Load Cell 1'
plotTitle2 = 'Displacement VS Time';
xLabel2 = 'Elapsed Time (s)';
yLabel2 = 'Displacement (mm)';
legend2 = 'Displacement Sensor 1'
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
yMax1 = 10000000
yMin1 = 0
yMax2 = 10000000
yMin2 = 0
plotGrid = 'on';
min1 = 0;
max1 = 10000;
min2 = 0;
max2 = 10000;
delay = .001;
time = 0;
data1 = 0;
data11 = 0;
data12 = 0;
data2 = 0;
data21 = 0;
data22 = 0;
count = 0;
subplot(2,4,1)
plotGraph1 = plot(time,data1,'-r')
subplot(2,4,2)
plotGraph2 = plot(time,data2,'-r')
hold on
title(plotTitle1,'FontSize',5);
xlabel(xLabel1,'FontSize',5);
ylabel(yLabel1,'FontSize',5);
axis([yMin1 yMax1 min1 max1]);
title(plotTitle2,'FontSize',5);
xlabel(xLabel2,'FontSize',5);
ylabel(yLabel2,'FontSize',5);
axis([yMin2 yMax2 min2 max2]);
grid(plotGrid);
tic
figure (1)
while ishandle(plotGraph1)
dat1 = read_HX711(loadcell)-1940.225269
count = count + 1;
time(count) = toc;
data1(count) = dat1(1);
set(plotGraph1,'XData',time,'YData',data1);
axis([0 time(count) min1 max1]);
pause(delay);
end
hold on
figure (2)
while ishandle(plotGraph2)
dat2 = readVoltage(a,'A0')*80
count = count + 1;
time(count) = toc;
data2(count) = dat2(1);
set(plotGraph2,'XData',time,'YData',data2);
axis([0 time(count) min2 max2]);
pause(delay);
end
hold off
delete(a);
disp('Plot Closed and arduino object has been deleted');

Sign in to comment.

Categories

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