Go Down

Topic: Matlab plot multiple columns of data from arduino (Read 4070 times) previous topic - next topic

113256

Jul 06, 2014, 03:47 am Last Edit: Jun 14, 2015, 01:19 pm by robtillaart
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:

Code: [Select]

#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)
Code: [Select]


delete(instrfindall);
clear all; clc; close all;

% Initialize serial port
s = serial('COM5','BaudRate',9600);   
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;


title('Incoming 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 %f', 2);
      voltage = data(i,1);
      temperature = data(i,1);


      % Plot the data
     plot(i, temperature, '*r', i, voltage, '--g');
      % 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

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

PaulS

Quote
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?

Since your problem is not with the Arduino sending data, but with matlob consuming data, I suggest that you ask at the matlab forum.
The art of getting good answers lies in asking good questions.

andrezitofer

You are overwriting the 'voltage data' with the 'Temperature data' and here is the problem:

     voltage = data(i,1);
     temperature = data(i,1);          <-------ERROR

The best way to do that is not to overwrite, and second, place the temperature data in another 'row', like this:

     voltage = data(i,1);
     temperature = data(i,2);

...just that. I hope this information be useful for all of the viewers of this topic too.


robtillaart

think you need to wait in matlab until a whole line has come in e.g. wait until a \n arrives.

Serial communication is like a dropping faucet, data drops in 1 byte at a time with some time in between. It cannot fill a bucket at once.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up