필터 지우기
필터 지우기

ploting two function on the same graph and the values get updated in the while loop

조회 수: 2 (최근 30일)
Hallo Everyone
I would like to draw two functions on the same graph and the values of the x1,x2 and y1,y2 gets updated in the while loop to give me real-time-graph alike. My code is working fine however i would like to insert the second function to the h1 and somehow i was not able to do so. For the graph i would like to have the x1 = timeStampsVec, y1= angVelVec , x2 = signalVec and y2 = punktVec . Can anyone please help me. I have wrote the whole code here to have a better image and understanding of what i am trying to do:
line 47
line 49
line 54
line 56
Thank you in advance
clear;
clc;
% Ein Arduino-Objekt erstellen
a = arduino('COM16', 'Nano33BLE');
a1 = serialport('COM14', 9600);
imu = lsm9ds1(a, 'SampleRate', 200, 'SamplesPerRead', 1, 'Bus', 1, 'OutputFormat', 'matrix','TimeFormat','duration');
%Eingangspuffer und Ausgangspuffer leeren
flush(a1);
% Initialisierung des Plots:
figure;
subplot(1, 1, 1)
h1 = plot(0, 0);
xlabel('Zeit (s)');
ylabel('Winkelgeschwindigkeit (rad/s)');
grid on;
ylim([-3 3]);
% Initialisierung der Vektoren:
t = datetime('now');
angVelVec = 0;
accelVec = 0;
signalVec = 0;
timeStampsVec = 0;
punktVec=[];
punkte=1
%ttttt
signalref = str2double(readline(a1));
% Schleife zum kontinuierlichen Lesen des Signals von D2 und zur Aktualisierung der Darstellung
tic;
startzeit = tic;
while toc(startzeit) < 10
[accel, gyro, mag, timeStamps, overrun] = imu.read;
% Siganlzeit aus milis() function
if a1.NumBytesAvailable > 0
signal1 = str2double(readline(a1));
signal2 = signal1 -signalref
signalVec = [signalVec signal2];
punktVec=[punktVec punkte];
disp(signal2);
end
% Aktualisierung der Signalvektoren
angVelVec = [angVelVec gyro(2)]; % um die x-Achse
accelVec = [accelVec accel(3)];
timeStampsVec = [timeStampsVec second(timeStamps)];
% Aktualisieren der Plots
if isvalid(h1)
set(h1, 'XData', timeStampsVec, 'YData', angVelVec);
end
drawnow;
end

답변 (1개)

Shubham
Shubham 2023년 6월 5일
Hi Taha,
To add a second function to the plot, you can simply define a new variable for the second plot and use the "hold on" command to plot both functions on the same figure. Here is the modified code:
clear;
clc;
% Ein Arduino-Objekt erstellen
a = arduino('COM16', 'Nano33BLE');
a1 = serialport('COM14', 9600);
imu = lsm9ds1(a, 'SampleRate', 200, 'SamplesPerRead', 1, 'Bus', 1, 'OutputFormat', 'matrix','TimeFormat','duration');
%Eingangspuffer und Ausgangspuffer leeren
flush(a1);
% Initialisierung des Plots:
figure;
subplot(1, 1, 1)
h1 = plot(0, 0);
xlabel('Zeit (s)');
ylabel('Winkelgeschwindigkeit (rad/s)');
grid on;
ylim([-3 3]);
% Initialisierung der Vektoren:
t = datetime('now');
angVelVec = 0;
accelVec = 0;
signalVec = 0;
timeStampsVec = 0;
punktVec=[];
punkte=1
%ttttt
signalref = str2double(readline(a1));
% Schleife zum kontinuierlichen Lesen des Signals von D2 und zur Aktualisierung der Darstellung
tic;
startzeit = tic;
% Define a new variable for the second plot
h2 = animatedline('Color','r');
while toc(startzeit) < 10
[accel, gyro, mag, timeStamps, overrun] = imu.read;
% Siganlzeit aus milis() function
if a1.NumBytesAvailable > 0
signal1 = str2double(readline(a1));
signal2 = signal1 -signalref
signalVec = [signalVec signal2];
punktVec=[punktVec punkte];
disp(signal2);
end
% Aktualisierung der Signalvektoren
angVelVec = [angVelVec gyro(2)]; % um die x-Achse
accelVec = [accelVec accel(3)];
timeStampsVec = [timeStampsVec second(timeStamps)];
% Aktualisieren der Plots
if isvalid(h1)
set(h1, 'XData', timeStampsVec, 'YData', angVelVec);
end
% Add second function to the same plot
if isvalid(h2)
addpoints(h2, signalVec,punktVec);
end
% Enable hold on command to keep both functions
hold on
drawnow;
end
I hope this helps!

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by