Two graphs at the same time

3 次查看(过去 30 天)
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'
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');

采纳的回答

darova
darova 2019-4-20
Try to use subplot() each time you want plot or modify something in figure
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')
title(plotTitle2,'FontSize',5);
xlabel(xLabel2,'FontSize',5);
ylabel(yLabel2,'FontSize',5);
axis([yMin2 yMax2 min2 max2]);
grid(plotGrid);
tic
% figure (1) % already created with "plotGraph1 = plot(time,data1,'-r')"
subplot(2,4,1), hold on
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);
plot(time, data1, '-r')
axis([0 time(count) min1 max1]);
pause(delay);
end
hold off
subplot(2,4,1), hold on
% figure (2) % already created with "plotGraph2 = plot(time,data2,'-r')"
while ishandle(plotGraph2)
dat2 = readVoltage(a,'A0')*80
count = count + 1;
time(count) = toc;
data2(count) = dat2(1);
% set(plotGraph2,'XData',time,'YData',data2);
plot(time, data2, '-r')
axis([0 time(count) min2 max2]);
pause(delay);
end
hold off
  6 个评论
Ameen Mouazzen
Ameen Mouazzen 2019-4-21
编辑:Ameen Mouazzen 2019-4-21
Hello,
Thank you very much for your help.
For the first time, the two graphs are working simultaneously.
Me and my colleagues appreciate your help very much.
Two problems left:
  1. both graphs are showing the data results in dots not lines ( we need them to be as lines)
  2. when I stop the code from runnung the graph window keeps opening again
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 = -10000000000;
max1 = 10000000000;
min2 = -10000000000;
max2 = 10000000000;
delay = .001;
time = 0;
data1 = 0;
data11 = 0;
data12 = 0;
data2 = 0;
data21 = 0;
data22 = 0;
count = 0;
subplot(2,4,1), hold on
subplot(2,4,2), hold on
tic
n = 2000; % 2000 measurements
[time, data1, data2] = deal( zeros(1,n) );
for count = 1:n
dat1 = read_HX711(loadcell)-1940.225269
dat2 = readVoltage(a,'A0')*80
time(count) = toc;
data1(count) = dat1(1);
data2(count) = dat2(1);
subplot(2,4,1)
plot(time(count),data1(count),'.b');
% axis([0 time(count) min1 max1]);
subplot(2,4,2)
plot(time(count),data2(count),'.r');
% axis([0 time(count) min2 max2]);
drawnow
pause(delay);
end
subplot(2,4,1), hold off
subplot(2,4,2), hold off
delete(a);
disp('Plot Closed and arduino object has been deleted');
darova
darova 2019-4-21
To draw a line "plot" needs at least 2 points ( plot([x1 x2], [y1 y2]) )
subplot(2,4,1), hold on
subplot(2,4,2), hold on
tic
n = 2000; % 2000 measurements
[time, data1, data2] = deal( zeros(1,n) );
time(1) = 0;
data1(1) = read_HX711(loadcell)-1940.225269
data2(2) = readVoltage(a,'A0')*80
for count = 2:n
dat1 = read_HX711(loadcell)-1940.225269
dat2 = readVoltage(a,'A0')*80
time(count) = toc;
data1(count) = dat1(1);
data2(count) = dat2(1);
subplot(2,4,1)
plot([time(count-1) time(count)], ...
[data1(count-1) data1(count)],'.-b');
% axis([0 time(count) min1 max1]);
subplot(2,4,2)
plot([time(count-1) time(count)], ...
[data2(count-1) data2(count)],'.-r');
% axis([0 time(count) min2 max2]);
drawnow
pause(delay);
end
subplot(2,4,1), hold off
subplot(2,4,2), hold off
delete(a);
disp('Plot Closed and arduino object has been deleted');
Which way do you stop the code? Closing figure or script is not enough
Use Ctrl+C

请先登录,再进行评论。

更多回答(1 个)

Ameen Mouazzen
Ameen Mouazzen 2019-4-21
编辑:Ameen Mouazzen 2019-4-21
Thank you very much for the reply.
Everything now is working correctly.
The Ctrl+C command worked.
New problems:
  • the graphs are showing no lables at all
  • the graphs take only half the screen. is there a way to make them bigger?
  • no gridlines shown in the graphs
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 = -10000000000;
max1 = 10000000000;
min2 = -10000000000;
max2 = 10000000000;
delay = .001;
time = 0;
data1 = 0;
data11 = 0;
data12 = 0;
data2 = 0;
data21 = 0;
data22 = 0;
count = 0;
subplot(2,4,1), hold on
subplot(2,4,2), hold on
tic
n = 2000; % 2000 measurements
[time, data1, data2] = deal( zeros(1,n) );
time(1) = 0;
data1(1) = read_HX711(loadcell)
data2(2) = readVoltage(a,'A0')
for count = 2:n
dat1 = read_HX711(loadcell)-1940.225269
dat2 = readVoltage(a,'A0')*80
time(count) = toc;
data1(count) = dat1(1);
data2(count) = dat2(1);
subplot(2,4,1)
plot([time(count-1) time(count)], ...
[data1(count-1) data1(count)],'.-b');
% axis([0 time(count) min1 max1]);
subplot(2,4,2)
plot([time(count-1) time(count)], ...
[data2(count-1) data2(count)],'.-r');
% axis([0 time(count) min2 max2]);
drawnow
pause(delay);
end
subplot(2,4,1), hold off
subplot(2,4,2), hold off
delete(a);
disp('Plot Closed and arduino object has been deleted');
  3 个评论
Ameen Mouazzen
Ameen Mouazzen 2019-4-21
编辑:Ameen Mouazzen 2019-4-21
Yes I agree,
But I'm studying civil engineering which has nothing to do with MATLAB. I started using MATLAB about 1 week ago. Before that time I didn't know anything about coding. And I've learnt a lot of things during this time. But my only concern is to make it work even If I didn't know how it works. becuase it is not related to my major. My graduation project submission is next week. Thats why I really don't have the time to learn everything before I start. But I'm trying my best.
Anyways, thank you very much for your help.
Ameen Mouazzen
Ameen Mouazzen 2019-4-21
编辑:Ameen Mouazzen 2019-4-21
I found a problem in factoring (dat1)
dat1 should be substracted from 8411800 and then multiplied by 0.000462214
as follows:
(dat1-8411800)*0.000462214
the brackets are important
have been trying for an hour
here is the script
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'
plotTitle2 = 'Displacement VS Time';
xLabel2 = 'Elapsed Time (s)';
yLabel2 = 'Displacement (mm)';
legend2 = 'Displacement Sensor'
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 2 2]);
yMax1 = 10000000
yMin1 = 0
yMax2 = 10000000
yMin2 = 0
plotGrid = 'on';
min1 = 0;
max1 = 1000;
min2 = 0;
max2 = 1000;
delay = .001;
time = 0;
data1 = 0;
data11 = 0;
data12 = 0;
data2 = 0;
data21 = 0;
data22 = 0;
count = 0;
subplot(1,2,1), hold on
subplot(1,2,2), hold on
tic
n = 2000;
[time, data1, data2] = deal( zeros(1,n) );
time(1) = 0;
data1(1) = read_HX711(loadcell)
data2(2) = readVoltage(a,'A0')
for count = 2:n
dat1 = read_HX711(loadcell)-8411800*(-0.000462214)
dat2 = readVoltage(a,'A0')*90
time(count) = toc;
data1(count) = dat1(1);
data2(count) = dat2(1);
subplot(1,2,1)
xlabel(xLabel1,'FontSize',15);
ylabel(yLabel1,'FontSize',15);
plot([time(count-1) time(count)], ...
[data1(count-1) data1(count)],'.-b');
subplot(1,2,2)
xlabel(xLabel2,'FontSize',15);
ylabel(yLabel2,'FontSize',15);
plot([time(count-1) time(count)], ...
[data2(count-1) data2(count)],'.-r');
drawnow
pause(delay);
end
subplot(1,2,1), hold off
subplot(1,2,2), hold off
delete(a);
disp('Plot Closed and arduino object has been deleted');

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by