필터 지우기
필터 지우기

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 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