I'm trying to send data from matlab to arduino via serail port using this code in Matlab:

 Arduino= serial ('COM4','BaudRate',9600);
while 1

and this in Arduino IDE:

int incoming;
void setup() {
void loop() {
  if (Serial.available()) {
    incoming = Serial.read();
    Serial.print("I received: ");
    Serial.println(incoming, DEC);

I've connected my Arduino Uno. "seriallist" command on matlab says:

ans = 

I'm getting this error in matlab:

Error using serial/fopen (line 72) Open failed: Port: COM4 is not available. No ports are available. Use INSTRFIND to determine if other instrument objects are connected to the requested device.

What's wrong with the serial port? Why matlab can't open it?

  • We always want to get rid of the error, don't we? I added my question. – 2012User Apr 28 '19 at 22:26
  • Can you open it in the IDE? Do you have it open in the IDE at the same time you're trying to use it in Matlab? – Majenko Apr 28 '19 at 22:29
  • 1
    Yes, Arduino IDE is open too. – 2012User Apr 28 '19 at 22:44
  • 1
    No ofcourse not. Serial monitor is not open. – 2012User Apr 28 '19 at 23:38
  • 1
    I kept trying, it works , but not in the while loop. – 2012User Apr 29 '19 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.