Matlab real-time plotting

Discussion in 'Programmer's Corner' started by Dritech, Mar 30, 2016.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi,

    I am using the code below to plot real-time signal from a microcontroller ADC. How can I modify this Matlab code so that I can plot two ADC signals instead of one?

    I did the following changes, but it is not working:

    * Modification 1: Changed " data = 0; " to " data = zeros(2,1); " so that I set an array of two to hold both ADC signals

    * Modification 2: Changed " dat = fscanf(s,'%f'); " to " dat = fscanf(s,'%f,%f'); " to receive both signals

    * Modification 3: Changed " data(count) = dat(1); " to " data(count, : )= dat'; "


    Code (Matlab M):
    1. % Serial Data Logger
    2. % Yu Hin Hau
    3. % 7/9/2013
    4. % **CLOSE PLOT TO END SESSION
    5. %User Defined Properties
    6. serialPort = 'COM5';            % define COM port #
    7. plotTitle = 'Serial Data Log';  % plot title
    8. xLabel = 'Elapsed Time (s)';    % x-axis label
    9. yLabel = 'Data';                % y-axis label
    10. plotGrid = 'on';                % 'off' to turn off grid
    11. min = -1.5;                     % set y-min
    12. max = 1.5;                      % set y-max
    13. scrollWidth = 10;               % display period in plot, plot entire data log if <= 0
    14. delay = .01;                    % make sure sample faster than resolution
    15. %Define Function Variables
    16. time = 0;
    17. data = 0;
    18. count = 0;
    19. %Set up Plot
    20. plotGraph = plot(time,data,'-mo',...
    21.                 'LineWidth',1,...
    22.                 'MarkerEdgeColor','k',...
    23.                 'MarkerFaceColor',[.49 1 .63],...
    24.                 'MarkerSize',2);
    25.            
    26. title(plotTitle,'FontSize',25);
    27. xlabel(xLabel,'FontSize',15);
    28. ylabel(yLabel,'FontSize',15);
    29. axis([0 10 min max]);
    30. grid(plotGrid);
    31. %Open Serial COM Port
    32. s = serial(serialPort)
    33. disp('Close Plot to End Session');
    34. fopen(s);
    35. while ishandle(plotGraph) %Loop when Plot is Active
    36.    
    37.     dat = fscanf(s,'%f'); %Read Data from Serial as Float
    38.  
    39.     if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct      
    40.         count = count + 1;  
    41.         time(count) = toc;    %Extract Elapsed Time
    42.         data(count) = dat(1); %Extract 1st Data Element      
    43.        
    44.         %Set Axis according to Scroll Width
    45.         if(scrollWidth > 0)
    46.         set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data(time > time(count)-scrollWidth));
    47.         axis([time(count)-scrollWidth time(count) min max]);
    48.         else
    49.         set(plotGraph,'XData',time,'YData',data);
    50.         axis([0 time(count) min max]);
    51.         end
    52.        
    53.         %Allow MATLAB to Update Plot
    54.         pause(delay);
    55.     end
    56. end
    57. %Close Serial COM Port and Delete useless Variables
    58. fclose(s);
    59. clear count dat delay max min plotGraph plotGrid plotTitle s ...
    60.         scrollWidth serialPort xLabel yLabel;
    61. disp('Session Terminated...');
     
Loading...