Matlab real time ploting multiple data from serial port

13,619

I actually used the code from the same source you are for one of my projects. If you still need assistance, here is my modified version of the code I used to plot three data lines from an accelerometer. It plots beautifully. However, because of the time it takes to do fscanf(), you can't obtain and plot a signal in real time faster than every 50ms or so without aliasing.

clear
clc

%User Defined Properties 
serialPort = 'COM7';            % define COM port #
plotTitle = 'Serial Data Log';  % plot title
xLabel = 'Elapsed Time (s)';    % x-axis label
yLabel = 'Acceleration';                % y-axis label
plotGrid = 'on';                % 'off' to turn off grid
min = -1.5;                     % set y-min
max = 2.5;                      % set y-max
scrollWidth = 10;               % display period in plot, plot entire data log if <= 0
delay = .0000001;                    % make sure sample faster than resolution

%Define Function Variables
time = 0;
data = zeros(3,1);
count = 0;

%Set up Plot
plotGraph = plot(time,data(1,:),'-r',...
            'LineWidth',2,...
            'MarkerFaceColor','w',...
            'MarkerSize',2);
hold on
plotGraph1 = plot(time,data(2,:),'-m',...
            'LineWidth',1,...
            'MarkerFaceColor','w',...
            'MarkerSize',2);
hold on
plotGraph2 = plot(time,data(3,:),'-b',...
            'LineWidth',1,...
            'MarkerFaceColor','w',...
            'MarkerSize',2);

title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);

%Open Serial COM Port
s = serial(serialPort, 'BaudRate', 115200)
disp('Close Plot to End Session');
fopen(s);

tic

while ishandle(plotGraph) && ishandle(plotGraph2) && ishandle(plotGraph1)  %Loop when Plot is Active

dat = fscanf(s,'%f'); %Read Data from Serial as Float

if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct        
    count = count + 1;    
    time(count) = toc;    %Extract Elapsed Time in seconds
    data(:,count) = dat(:,1); %Extract 1st Data Element         

    %Set Axis according to Scroll Width
    if(scrollWidth > 0)
    set(plotGraph,'XData',time(time > time(count)-scrollWidth),...
        'YData', data(3,time > time(count)-scrollWidth));
    set(plotGraph1,'XData',time(time > time(count)-scrollWidth),...
        'YData', data(2,time > time(count)-scrollWidth));
    set(plotGraph2,'XData',time(time > time(count)-scrollWidth),...
        'YData', data(1,time > time(count)-scrollWidth));

    axis([time(count)-scrollWidth time(count) min max]);
    else
    set(plotGraph,'XData',time,'YData',data(3,:));
    set(plotGraph1,'XData',time,'YData',data(2,:));
    set(plotGraph2,'XData',time,'YData',data(1,:));

    axis([0 time(count) min max]);
    end

    %Allow MATLAB to Update Plot
    pause(delay);
end
end

%Close Serial COM Port and Delete useless Variables
fclose(s);

clear count dat delay max min plotGraph plotGraph1 plotGraph2 plotGrid...
    plotTitle s scrollWidth serialPort xLabel yLabel;

disp('Session Terminated');

prompt = 'Export Data? [Y/N]: ';
str = input(prompt,'s');
if str == 'Y' || strcmp(str, ' Y') || str == 'y' || strcmp(str, ' y')
    %export data
    csvwrite('accelData.txt',data);
    type accelData.txt;
else
end

clear str prompt;
Share:
13,619
hanga mihai
Author by

hanga mihai

Updated on June 05, 2022

Comments

  • hanga mihai
    hanga mihai almost 2 years

    My application reads data from sensors trough an ARDUINO UNO platform and then trough serial port I managed to read all the data that I need in MATLAB. so now I have 3 datas that I want to plot (data, data2, data3) in real time ON THE SAME GRAPHIC.

    I also managed to plot one data at a time with some code I found on mathworks and modified it a bit, which does not suit my project.

    Here is the matlab code that I'm using to plot one of the data:

    clear
    clc
    
    %User Defined Properties 
    serialPort = 'COM7';           % define COM port #
    baudeRate = 115200;
    plotTitle = 'Serial Data Log';  % plot title
    xLabel = 'Elapsed Time (s)';    % x-axis label
    yLabel = 'Data';                % y-axis label
    plotGrid = 'on';                % 'off' to turn off grid
    min = -200;                     % set y-min
    max = 200;                      % set y-max
    scrollWidth = 10;               % display period in plot, plot entire data log if <= 0
    delay = .01;                    % make sure sample faster than resolution
    
    %Define Function Variables
    time = 0;
    data = 0;
    data2 = 0;
    data3 = 0;
    count = 0;
    
    %Set up Plot
    plotGraph = plot(time,data,time,data2,time,data3);
    
    title(plotTitle,'FontSize',25);
    xlabel(xLabel,'FontSize',15);
    ylabel(yLabel,'FontSize',15);
    axis([0 10 min max]);
    grid(plotGrid);
    
    %Open Serial COM Port
    s = serial(serialPort, 'BaudRate',baudeRate)
    disp('Close Plot to End Session');
    fopen(s);
    
    tic
    
    while ishandle(plotGraph) %Loop when Plot is Active
    
        dat = fscanf(s,'%f'); %Read Data from Serial as Float
    
        if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct        
            count = count + 1;    
            time(count) = toc;    %Extract Elapsed Time
            data(count) = dat(1); %Extract 1st Data Element  
            data2(count) = dat(2);
            data3(count) = dat(3);
    
            data(count);
            data2(count);
            data3(count);
    
            %Set Axis according to Scroll Width
            if(scrollWidth > 0)
            set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
            %plot(time(time > time(count)-scrollWidth),data3(time > time(count)-scrollWidth));
            axis([time(count)-scrollWidth time(count) min max]);
            %set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
            %axis([time(count)-scrollWidth time(count) min max]);
            else
            set(plotGraph,'XData',time,'YData',data);
            axis([0 time(count) min max]);
            end
    
            %Allow MATLAB to Update Plot
            pause(delay);
        end
    end
    
    %Close Serial COM Port and Delete useless Variables
    fclose(s);
    clear count dat delay max min baudRate plotGraph plotGrid plotTitle s ...
            scrollWidth serialPort xLabel yLabel;
    
    
    disp('Session Terminated...');
    

    I need to plot all the 3 datas (data, data2, data3) on the graphic with different colors. Please help me out here.

  • pranjal khanduri
    pranjal khanduri almost 7 years
    what is the format of input in your code?the three axis value of the accelerometer.