How to control the Arduino with MATLAB in realtime

I want to read the data from arduino to Matlab and send the result from Matlab to arduino in realtime. How to do it?
I know now:
In Matlab:
s= serial(‘COM3’, ‘BaudRate’, 9600, ‘Parity’, ‘none’, ‘DataBits’, 8);
(caculate in MATLAB)
fscantf(s, ‘1’); (send the result back to Arduino)

In Arduino

Serial.print(‘to send something back to matlab’);

1.I try to read the data from arduino in matlab, but always “Warning:A timeout occurred before the Terminator was reached.” How to solve it?
2.How to use Arduino to read the data from Matlab?

Arduino read data <-----------------------------------------------
| |
| |
v |
Matlab read the data from arduino and calculate |
| |
| |
v |
Arduino read information from Matlab and do something—


2.How to use Arduino to read the data from Matlab?

On the Arduino, you use Serial.begin() in setup() to start using the serial port.

To read data sent by MATLAB, use Serial.available() to see how much data is available, and to read the data one byte at a time.

1.I try to read the data from arduino in matlab, but always "Warning:A timeout occurred before the Terminator was reached." How to solve it?

We'd need to see the sketch that was being used to send the data to the serial port, and the MATLAB code that was trying to read the data.

code in Arduino:

double IR; 
void setup() {   
void loop() {   
        IR1    = read_gp2d12_range(1); 
        IR2    = read_gp2d12_range(2); 
if (
    digitalWrite(1, HIGH);
else if (
    digitalWrite(1, LOW);
float read_gp2d12_range(byte pin) { 
        int tmp; 
        double value;
        tmp = analogRead(pin); 
        value=(6787.0 /((float)tmp - 3.0)) - 4.0;
        if (tmp < 4) return 0;            
        else if (value < 4.0 || value >90.0) return 0.0;
        return value; 

Code in MATLAB:

s= serial('COM3', 'BaudRate', 9600, 'Parity', 'none', 'DataBits', 8);
[s1 s2] = size(z);
x = (1:2160)';
title('Incomming Data from Arduino');
if s1>=2160;
fitlin = 'F_linear(x, a,b)';
fitfunc='(c-d)*F_sigmoid(x, a, b)+d';
initval= [0 10 50 0 ]
[fity1] = fit(x, y(:,1), fittype(fitfunc),...
[fity1_1]= fit(x, y(:,1), fittype(fitlin),...
                     fitoptions('Method','NonLinear','Start',[Lmax Lmin]));
plot(fity1, x, y(:,1),'b.'); hold on;
plot(fity1_1);hold on;
plot(g1, fity1(g1),'k*');hold on;

[fity2] = fit(x, y(:,2), fittype(fitfunc),...
[fity2_1]= fit(x, y(:,2), fittype(fitlin),...
                     fitoptions('Method','NonLinear','Start',[Vmax Vmin]));
plot(fity2, x, y(:,2),'b.'); hold on;
plot(fity2_1);hold on;
plot(g2, fity2(g2),'k*');hold on;

if (fity1.a<2160 && fity1_1.a<-1)
    fprintf(s, '1');
if (fity2.a>0 && fity2.a<2160 && fity2_1.a>0.3)
    fprintf(s, '0');

I use Arduino to read data 10(s) once, and do curvefitting in matlab 6(hr) (It’s a slide window). write a value back to arduino and trigger motor.

Where are IR1 and IR2 declared in the Arduino code?

You don’t have the Serial Monitor window open while the MATLAB code is running, do you?
COM3 is the port that the Arduino is connected to, right?

Do you know that s is created?
Do you know that fopen succeeded?
Is fscanf how you read from the serial port in MATLAB? Is that the function that times out?

double IR1,IR2;

I didn't use the serial monitor window when I ran the MATLAB code.

s was created, fopen also succeeded.

A better question would be: how does one afford MATLAB????

[edit]Sorry.. I'm being ridiculous. :-X[/edit]

        [glow]IR1[/glow]    = read_gp2d12_range(1);

Probably ought to fix this, even though it isn't really related to the problem.

Opening a serial connection to the Arduino causes it to reset. It takes some time before the Arduino is ready to start sending serial data again. Perhaps the issue if that MATLAB is timing out before the Arduino has sent any data.

Can you change the relationship between when the socket is opened and the read is performed, or introduce a delay before reading from the serial port after the connection is made, or change the timeout length?

A better question would be: how does one afford MATLAB????

For all the thousands of dollars that are spent for matlab/Labview type products, there appears to be zero customer support for using the products. I see a lot of serial I/O questions concerning these products, with the solutions seeming to be in the "best guess" catagory.

Hello! I’m trying an program to communicate Matlab with Arduino and when I try to sent Matlab to arduino with fprintf there’s no way, timeout occurs…

here is the matlab:

function comunicacio(~)

s = serial(‘COM3’,‘BaudRate’,9600);
out = fscanf(s);
if strcmp(out,‘N’)
for i=1:str2double(out)

and here the arduino’s one

int pin = 13;

void setup()


void loop()
if (Serial.available() > 0) {;
digitalWrite(ledPin, HIGH);
for(int k=0;k<tmp;k++){

Thank you!

Anybody able to provide the block diagram or the circuit diagram for the above project do i have to use vc++ for pluggin of the micrcontroller

I want trigger a dc voltage from the PWM of the arduino once an event has been detected ;provide an analog o/ to do this ....

I am having the same problem trying to interface with Matlab. All the geniuses out there. We need your help. My code stops reading the serial port and telling me there is no USB connected when it was reading it just a sec ago. I think the Arduino community will be the only help. Matlab don't offer any kind of help whatsoever.

A better question would be: how does one afford MATLAB????

If you are in college, it's like $130.

Can't you just download it from :D

Working with the MATLAB serial object is tricky and one source of problems is the terminator definition. The MATLAB help has a number of methods to work with the serial object to check for bytes available and callback functions.

I would suggest capturing your text with putty and saving it to a file. Then use pspad in hex mode to look at what the arduino is sending and then set MATLAB serial object property terminator to match the arduino terminator. Usually on windows it's hex OD and OA for carriage return and linefeed.

There is an open source matlab like software called octave but the last time I checked the serial object was only in the linux version. Another solution path would be to use python with numpy and scipy.

Have fun measuring stuff!