Go Down

Topic: Arduino to MATLAB (Read 9674 times) previous topic - next topic



I want to pass some accelerometer and gyro signals through an Arduino NG into MATLAB for analysis. Does anyone have an API for this? MATLAB does not currently support Arduino.

Thanks, any thoughts on this would be helpful.



Is this something you could do by loading a properly-formatted text file into MATLAB?  I'm speaking with utter ignorance of what MATLAB allows you to do... I mean, I know it has something to do with math, but other than that... EEK!  :)

Anyway, perhaps you could Serial.print() properly formatted text, capture that text with a serial terminal program, save to a file, then load that into MATLAB?


You should just be able to get MATLAB to open the serial port and listen to incoming data. We did this using other serial sensors back in the Uni days, but I don't have a working version of MATLAB anymore to test it with Arduino.


last year I had to search the web quite a lot on this subject. I needed to analyze an analog signal coming from a couple of hall effect sensors. I used an arduino for the A/D convertion of both sensors. As I donĀ“t have Matlab's "serial communication" toolbox (don't know If it's called like that, but I guess it is..) I decided to try with scilab (an open source Matlab-like application). There is a scilab function here that may help you:


it's called serial.dll, just select your COM port, bit rate,  byte length, parity, etc, and you will be able to  reciebe the data. scilab uses almost the same commands as Matlab.
I know it's not exactly wat you wanted, but give it a try if you have no other options

P.S:  http://www.scilab.org/

good luck


I was wondering if any progress was made on this issue. I was hoping to send some ADC values to MATLAB.


same here, I'd be very interested in this!




MATLAB has a serial library, unless you're using a mac. Info in the MATLAB docs http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f105659.html. If like me you're using a mac, you're pretty stuffed, as 2007R1 and 2008R1 broke the 3rd party mac serial library. Hope this helps.


thank you very much! I'll see what I can do with this when I have time.
Matlab is one of the reasons I am running bootcamp on my mac...




I had the same problem and managed to solve it using the following generic Matlab Code to read from the Serial Port:

Code: [Select]

%Basic Comm
s = serial('COM5');
out = fscanf(s);
clear s

On the other hand I did some code to read signals from the ADXL30 with the arduino.
I programmed the arduino so that it would send a serial stream as the following
X<accX> [space] Y <accY> [space] Z [accZ] [CR]
where <accX> is the value of the A/D comming from the accelerometer.
I enclose the MATLAB side,

Code: [Select]

%Graph Accelerometer Data

s = serial('COM5');

out ='a'
hold on
xacc =[];
yacc =[];
zacc =[];
for i = 1 : 5000
   out = fscanf(s);
       out2 = reshape(out(1:(round(size(out,2)/5)-1)*5),[5 (round(size(out,2)/5)-1)]);
       out2 = out2';
       out2 = str2num(out2(1:end,2:end));
       if out(1) == sprintf('X')
           xacc = [xacc out2(1:3:end)'];
           yacc = [yacc out2(2:3:end)'];
           zacc = [zacc out2(3:3:end)'];

clear s

This code only reads the signals from the serial port, stores them in different variables (depending on the axis) and plots them.

The main problem with Matlab ,though, is that is not fast enough to process all the datam, you'll see (if you try the code) that the graphs are drawn pretty slow (I got a celeron but I do not think that's the problem.

If you are interested in the Arduino code for that, let me know.




did you use the code? let me know if it helped!!!

I will be posting the arduino part here https://webfiles.uci.edu/cagell/news/news.htm.



Reading serial and writing to serial is very easy in matlab, but it sometimes hang, serial port already in use and then u need to restart matlab.  

The matlab documentation is very good, just type doc serial.

Code: [Select]

s = serial('COM1','BAUD',4800);
fid = fopen(s);

%do whatever u want to fid here.....


Hello, I'm looking to use matlab to interact with a Diecimla board.  I think its on serial com port 3 on my machine, but i'm not quite sure where to go from here.

for a test I was thinking of maybe having some kind of random number generator uploaded onto the board, then send that as the output.

My goal here is to use matlab to control an input to the board via the usb.  But just a little help with my beginner problem would be enough for me to figure the rest out hopefully.


Never mind I'll find something else... This isn't going to work. [smiley=sad.gif]


If I have time before school starts up again, I'll try to get Matlab talking with Firmata.  I would also like to see a Firmata library for Labview.  If anyone else has time, please start and post your progress in the playground!


Dolphin: I tried your code. The Arduino Side didnt work really, I hat to change printByte to Serial.print and came to the following piece of code:

Code: [Select]
int ledPin = 13;
int xaccPin = 0;
int yaccPin = 1;
int zaccPin = 2;
int value = 0;

void setup() {
 //beginSerial(9600); // Sets the baud rate to 9600

 pinMode(ledPin, OUTPUT);
 pinMode(xaccPin, INPUT);
 pinMode(yaccPin, INPUT);


void readAcceleration(int axe){
 value = analogRead(axe);

 if (axe == xaccPin){
 if (axe == yaccPin){
 if (axe == zaccPin){

 /* printInteger(value); */
 Serial.print(' ',BYTE);

void loop() {
 readAcceleration(xaccPin); //reads and represents acceleration X
 readAcceleration(yaccPin); //reads and represents acceleration Y
 readAcceleration(zaccPin); //reads and represents acceleration Y
 digitalWrite(ledPin, HIGH);
 digitalWrite(ledPin, LOW);


The code for Matlab did something to the serial port: The Arduino stopped blinking as soon as I executed the code in Matlab.  And nothing was displayed in the graph. Whats the prob there?

Go Up