Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Utkarsha Chinchore
Utkarsha Chinchore le 15 Fév 2020
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 commentaire
Star Strider
Star Strider le 15 Fév 2020
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.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 15 Fév 2020
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);

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Objects dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by