フィルターのクリア

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

1 回表示 (過去 30 日間)
Utkarsha Chinchore
Utkarsha Chinchore 2020 年 2 月 15 日
回答済み: Image Analyst 2020 年 2 月 15 日
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 Center および File ExchangeGraphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by