1

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

2
  • Have you've been able to successfully retrieve and print the temp values in the while loop? If so you can use an animatedLine plot and the addpoints() function to update the graph upon each reading each sample. Commented Jan 6, 2021 at 20:16
  • I could retrieve the data just fine. I tried using 'animatedLine' and addpoints() however nothing changed. Commented Jan 6, 2021 at 21:13

1 Answer 1

1

*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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.