Friday, 23 January 2009

Plotting Real Time Data

In the earlier post, I've described how to communicate with an external MCU from your PC through MATLAB. Now the following section describes how to get real-time data from it and how to plot it on the screen.

The program captures two multiplexed data sent by MCU. It then demultiplex it and plot the values periodcally into the screen.

The received data is stored in a 100x2 matrix, data. It is then plotted.

%==================Pgm Starts=========================%
try
fclose(instrfind)
end

s1 = serial('COM1','BaudRate',2400,'Parity','none','DataBits',8,'StopBits',1);
set(s1,'InputBufferSize',1024);
fopen(s1)
disp('Serial Communication Set Up Complete');

%async mode
s1.Terminator = 13;
s1.Timeout = 10; %Default
s1.ReadAsyncMode = 'continuous';
%readasync(s1);

k = 1;

data = zeros(100,2);

PLOT = 100;

figure(1);

%=======================Plot Set Up and Grab Handles==================%

subplot(2,1,1);
FirstPlot = plot(data(:,1),'g');
hold on;
axis([1 100 0 5]);
title('First Plot');

subplot(2,1,2);
SecondPlot = plot(data(:,2),'b');
hold on;
axis([1 100 0 5]);
title('SecondPlot');

set(FirstPlot,'Erase','xor');
set(SecondPlot,'Erase','xor');


while(1)

data(k,:) = fread(s1,2,'uint8');

%============Updata pLots==================%

set(FirstPlot,'YData',data(:,1));
set(Secondlot,'YData',data(:,2));
drawnow;
%========================================%

k = k+1;

if(k==PLOT)
k=1;
end

end


querrymail@gmail.com

0 comments: