title: Matlab real time ploting multiple data from serial port
body: <p>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 <strong>ON THE SAME GRAPHIC</strong>.</p>
  
  <p>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.</p>
  
  <p>Here is the matlab code that I'm using to plot one of the data:</p>
  
  <pre><code>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 &lt;= 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) &amp;&amp; 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 &gt; 0)
          set(plotGraph,'XData',time(time &gt; time(count)-scrollWidth),'YData',data3(time &gt; time(count)-scrollWidth));
          %plot(time(time &gt; time(count)-scrollWidth),data3(time &gt; time(count)-scrollWidth));
          axis([time(count)-scrollWidth time(count) min max]);
          %set(plotGraph,'XData',time(time &gt; time(count)-scrollWidth),'YData',data3(time &gt; 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...');
  </code></pre>
  
  <p>I need to plot all the 3 datas (data, data2, data3) on the graphic with different colors. Please help me out here.</p>