8 years, 1 month ago.

Serial port to Matlab

Hi, I found the code below which I intend to use for plotting real-time analog data from a microcontroller. How can I modify the code so that I can plot five analog signals instead of just one? I know that I have to change dat = fscanf(s,'%f'); to dat = fscanf(s,'%f,%f,%f,%f,%f'); I also replaced data(count) = dat(1); with data(count,:) = dat'; but unfortunately it is not working. Any kind of help would be highly appreciated.

[CODE] % Serial Data Logger % Yu Hin Hau % 7/9/2013 % CLOSE PLOT TO END SESSION

clear clc

%User Defined Properties serialPort = 'COM5'; % define COM port # 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 = -1.5; % set y-min max = 1.5; % 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; count = 0;

%Set up Plot plotGraph = plot(time,data,'-mo',... 'LineWidth',1,... 'MarkerEdgeColor','k',... 'MarkerFaceColor',[.49 1 .63],... '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) 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

%Set Axis according to Scroll Width if(scrollWidth > 0) set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data(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 plotGraph plotGrid plotTitle s ... scrollWidth serialPort xLabel yLabel;

disp('Session Terminated...');

[/CODE]

Be the first to answer this question.