How to connect matlab to arduino

hi guys , i need help from everyone, i tried to learning connecting matlab to arduino,,lets say if i send 1 from matlab , the result is led on and otherwise is off.

my matlab code is
arduino=serial('COM3','BaudRate',9600); % create serial communication object on port COM3

a= 1;
sendData = dec2bin (a);
fopen(arduino);
fwrite(arduino,sendData);
fclose(arduino);

my arduino code

   int ledPin=13;
   int matlabdata;
   void setup()
   {
   pinMode(ledPin,OUTPUT);
   Serial.begin(9600);
   }
   void loop()
  
   {
   matlabdata=Serial.read(); // read data
   if(matlabdata == 1)
   digitalWrite(ledPin,HIGH); // turn light on
   else if (matlabdata == 2)
   digitalWrite(ledPin,LOW);} // turn off

my email is ryhan_trix@yahoo.com . thank you

In MATLAB, what is the point of this statement?

sendData = dec2bin (a);

For the Arduino, you should read up on how to use the serial routines. It doesn't make much sense to read the serial line until data are available.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Now I'm going to spend 10 minutes deleting all your other threads. Grrr.

  • Moderator

How to use this forum

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” button above the posting area (It looks like a scroll with < > inside it).

jremington: In MATLAB, what is the point of this statement?

sendData = dec2bin (a);

For the Arduino, you should read up on how to use the serial routines. It doesn't make much sense to read the serial line until data are available.

my friend told me to convert to binary first then send the data, i just try to. so uh what should i do then? i am sorry i am very new but i really want to understand.

[quote author=Nick Gammon link=msg=2104072 date=1424492317] Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Now I'm going to spend 10 minutes deleting all your other threads. Grrr.

  • Moderator

How to use this forum

[/quote]

yeah i am really sorry,,i am newbie in this forum,,i just try to get some help.

so uh what should i do then?

Read the documentation. Use Google, for example “connect arduino matlab”.