Communication between Matlab and Arduino and then from one Arduino to another

I want to send a simple data from Matlab to one Arduino connected with PC and then that arduino will send the received data to another arduino via xbee. To think it simply suppose I want to send '5' to Arduino 1. After receiving the '5', Arduino 1 will send that '5' to Arduino 2 via xbee connected with both the arduinos. I am not getting any data in the last Arduino. And while using by Matlab, serial monitor is locked; so I can't understand whether even Arduino 1 is getting the data from Matlab or not.

I checked the previous queries but didn't find anything suitable.

For Matlab:

s=serial('COM4');
fopen(s);
fwrite(s, '5');
fclose(s);

For Arduino 1:

void setup() {
Serial.begin(9600);
}

void loop(){
if(Serial.available()>0){
Serial.println(Serial.read());
  }

}

For Arduino 2:

void setup() {
Serial.begin(9600);
}

void loop(){
if(Serial.available()>0){
Serial.println(Serial.read());
  }

}

Are you trying to use the Serial instance to talk to Matlab OR to talk to the XBee? Both is the wrong answer. So is yes.

Thanks for your query.

Matlab is running in my PC, I connected Arduino-1+Xbee with my PC; And then powered another Arduino-2+Xbee by battery. So, did you mean sending Matlab command to connected Xbee+arduino is technically not possible? Will SoftwareSerial work?

Could you please let me know whether is it possible to use Matlab with Arduino+Xbee? Or I have to go for something else (rather than Matlab) to accomplish this?

So, did you mean sending Matlab command to connected Xbee+arduino is technically not possible?

Technically, it is feasible. But, MatLab sent you data. Why do you need to send that data back to MatLab?

You can't use one serial port to talk to two devices, and expect them to know when you are talking to each one.

Will SoftwareSerial work?

For what? To talk to the XBee? That depends on how the XBee is connected to the Arduino.

Could you please let me know whether is it possible to use Matlab with Arduino+Xbee?

Yes, it is possible.

MatLab sent you data. Why do you need to send that data back to MatLab?

I want to send one data [suppose like '1234'] to one arduino. Arduino will process the data and send it to other arduino via xbee. Matlab is not sending to two arduino+xbee.

For what? To talk to the XBee? That depends on how the XBee is connected to the Arduino.

I mentioned SoftwareSerial incase the arduino can't read command/data from Matlab while connected with Xbee. As you said it is possible; I think I should discard this idea. And for just the query my Series 1 Xbee is connected with Arduino UNO R3 via https://solarbotics.com/product/51835/.

I tried this simple blinking experiment; which works when only arduino is there but if I connect xbee, it is not working.

For Arduino:

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

For Matlab:

    clear all
    clc
     
    answer=1; % this is where we'll store the user's answer
    arduino=serial('COM4','BaudRate',9600); % create serial communication object on port COM4
     
    fopen(arduino); % initiate arduino communication
     
    while answer
    fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
    answer=input('Enter led value 1 or 2 (1=ON, 2=OFF, 0=EXIT PROGRAM): '); % ask user to enter value for variable answer
    end
     
    fclose(arduino); % end communication with arduino

How can I make the arduino to receive the blinking command even when it is connected with Xbee?

This above sketch works with SoftwareSerial in arduino. Thanks for your answer.

Hi all
i want to send 2 float number from matlab to arduino or an array includes some number.
please help me
thank you