Matlab plot multiple columns of data from arduino

13 views (last 30 days)
On matlab i have incoming serial data from the Arduino. The arduino reads data from one sensor and prints it onto the serial monitor and MATLAB plots a graph with it. When i print data from 2 sensors (separated by a tab) matlab doesnt plot a graph. How am i supposed to parse them and plot them both on the graph in real time?
arduino code:
#include <ColorLCDShield.h>
#include <math.h>
#include <float.h>
char data;
int counter = 0;
const double ARDUINO_VOLTAGE = 5.0;
int tempPin= A3;
int voltPin = A2;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int temp= analogRead(tempPin);
int voltage = analogRead(voltPin);
Serial.print(temp);
Serial.print("\t");
Serial.println(voltage);
delay(500);
}
so data is shown like this on the serial monitor:
temp voltage
temp voltage
temp voltage
.......
matlab code (only plots the temperature and not the voltage)
delete(instrfindall);
clear all; clc; close all;
% Initialize serial port
s = serial('COM5','BaudRate',9600);
%set(s, ' Terminator', 'LF'); % Default terminator is \n
set(s,'BaudRate', 9600);
set(s,'DataBits', 8);
set(s,'StopBits', 1);
fopen(s);
s.ReadAsyncMode = 'continuous';
% variables
numberOfData = 500;
data = zeros(1, numberOfData);
i = 1;
% Main graph figure
figure(1);
hold on;
grid on;
%axis auto;
title('Incomming Data from External Device');
xlabel('Time');
ylabel('temp');
% Start asynchronous reading
readasync(s);
while(i<=numberOfData)
% Get the data from the serial object
data(i) = fscanf(s, '%f');
% Plot the data
plot(i, data(i), '*');
% Draw and flush
drawnow;
%Increment the counter
i=i+1;
end
% Give the external device some time…
pause(3000);
return;
catch
% Some of these crash the program – it depends. The serial port is left
% open, which is not good.
stopasync(s);
fclose(s); % bad
delete(s);
clear s;
fprintf(1, 'Sorry, you"re going to have to close out of Matlab to close the serial port\n');
return
end
  1 Comment
dpb
dpb on 5 Jul 2014
I've never used serial i/o w/ Matlab so I'm surmising here, but...
If you're writing '%f\t%f' why aren't you reading that???
while(i<=numberOfData)
% Get the data from the serial object
data(i,:) = fscanf(s, '%f\t%f',1); % read two tab-delimited values
% NB: I don't know if the serial fscanf() will wait or just return
% if sufficient data aren't yet ready. Perhaps you'll need to
% check bytesAvailable before reading blindly???
% Plot the data
It would be better to update the xdata,ydata properties of the plot rather than keep calling plot over and over--every time you do that you create a new line object that contains only the one (or two) points.
See the section in Graphics on Animation for examples. But, see if the above lets you at least get the results back...

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!