How to plot a graph? I'm getting a blank graph.

1 次查看(过去 30 天)
clear
close all
clc
a = arduino('COM6','MEGA2560');
v=0;
i = 1;
while (i<50)
v = readVoltage(a,'A11');
if(v>2.2)
writeDigitalPin(a,'D13',0);
else
writeDigitalPin(a,'D13',1);
plot(i,v,"r")
ylim([0 5]);
xlim([0 5]);
hold on
i = i +1 ;
end
end
  1 个评论
Star Strider
Star Strider 2020-2-15
I cannot run your code, so I am not posting this as an Answer.
However, it appears that you are plotting in a loop, so plot with a marker instead:
plot(i,v,'pr')
This should produce a series of red stars.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2020-2-15
I'm not sure how long the loop takes, but it appears that it should take just milliseconds since I don't see any pause() command in there. So why can't you store all the values and plot them all at once after the loop? If it does take some time, you can put a drawnow inside the loop to force it to refresh the screen (otherwise it might just wait until it gets a chance):
clear
close all
clc
% Instantiate the arduino object.
arduinoObject = arduino('COM6','MEGA2560');
% Start polling the arduino device.
numPoints = 50; % Max number of points to get. The failsafe.
v = zeros(numPoints, 1);
i = 1;
while (i < numPoints)
% Read from the arduino pin.
v(i) = readVoltage(arduinoObject,'A11');
% Write to the arduino pin.
if(v(i) > 2.2)
writeDigitalPin(arduinoObject, 'D13', 0);
% Do we need to reset i to 1 here???
else
writeDigitalPin(arduinoObject, 'D13', 1);
plot(i, v(i), 'r.', 'MarkerSize', 30);
ylim([0, 5]);
xlim([0, 5]);
grid on;
hold on;
drawnow; % Force immediate screen refresh/repaint.
i = i + 1 ;
end
end
% Now finish up by plotting everything.
hold off;
cla;
plot(v, 'r-.', 'MarkerSize', 30);
ylim([0 5]);
xlim([0 5]);
grid on;
xlabel('Index', 'FontSize', 18);
ylabel('PinValue', 'FontSize', 18);
title('Arduino Signal', 'FontSize', 18);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by