Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

plot - Plotting live data using MATLAB

I'm trying to plot the live data that I'm getting from my sensor. I want the graph to always update when new data is detected. This is my current code. I can't get the line to display.

Thanks in advance.

Code:

clear all;
S = serialport('COM5',115200);
temp =[];
accx=[];
accy=[];
accz=[];
i = 0;
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)];
     plot(i,temp(1))
     i=i+1;
 end
end

Graph:

enter image description here

question from:https://stackoverflow.com/questions/65602576/plotting-live-data-using-matlab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...