*May or may not be useful to solving the problem
Given you can successfully retrieve the samples/values within the for-loop. Here is a playground script that adds values retrieved upon every iteration to a plot. This playground script adds a single value upon every loop within the while-loop. The hold on
term is used to keep the existing plot and the new values on the same current axes, gca
. If you retrieve multiple values on each iteration this script may require slight modifications. Here I slide the x-axis as the number of samples progresses, this isn't necessary but does kind of give an oscilloscope type feel.
Snippet of Plotting:
Pressing the command Ctrl + C with your mouse cursor in the command window can be used to terminate the script.
Playground Script:
clf;
%Initializing animated line plot%
Animated_Plot = animatedline;
Sample_Index = 1;
while true
%Retrieve sample (temp) value%
temp = sin(0.2*Sample_Index);
%Add points to the plot%
hold on
addpoints(Animated_Plot, Sample_Index, temp);
drawnow
%Window is the number of samples on screen/figure at a time%
Window = 50;
Sample_Chunk = floor(Sample_Index/Window);
Maximum_Amplitude = 1;
Minimum_Amplitude = -1.5;
axis([Sample_Chunk*Window Sample_Chunk*Window+Window Minimum_Amplitude Maximum_Amplitude]);
title("Plotting 50 Samples at a Time");
xlabel("Sample Index"); ylabel("Amplitude");
Sample_Index = Sample_Index + 1;
grid on
end
Full Script:
A full script might follow the lines of something like this. I have not tested this by reading a COM port yet:
clear all;
S = serialport('COM5',115200);
temp =[];
accx=[];
accy=[];
accz=[];
clf;
%Initializing animated line plot%
Animated_Plot = animatedline;
Sample_Index = 1;
while true
if S.NumBytesAvailable > 0
data = readline(S);
temp = [str2double(split(data,','))];
accx = [accx;temp(1)];
accy = [accy;temp(2)];
accz = [accz;temp(3)];
%Add points to the plot%
hold on
addpoints(Animated_Plot, Sample_Index, temp(1));
drawnow
%Window is the number of samples on screen/figure at a time%
Window = 50;
Sample_Chunk = floor(Sample_Index/Window);
Maximum_Amplitude = 1;
Minimum_Amplitude = -1.5;
axis([Sample_Chunk*Window Sample_Chunk*Window+Window Minimum_Amplitude Maximum_Amplitude]);
title("Plotting 50 Samples at a Time");
xlabel("Sample Index"); ylabel("Amplitude");
Sample_Index = Sample_Index + 1;
grid on
end
end
Ran using MATLAB R2019b
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…