Serial Communication With Matlab

I am using an Arduino UNO in a project at school. We are trying to communicate serially between the Arduino and Matlab. I am having issues getting communication between the two. My Matlab and Arduino code is shown below. If I step through the Matlab code using breakpoints, I get the exact behavior I would expect. Three bytes are transmitted from Matlab, and those three bytes are echoed back to Matlab by the Arduino. However, if I run the code without breakpoints then I don't get any response from the Arduino. I have verified that the Rx signal lights up on the Arduino when I am transmitting to it. Also, I have tried many different delays on both the Arduino end and the Matlab end. I have tried delays ranging anywhere from 0s - 2s.

The Matlab code on my 2.2GHz Core i7 is going to execute far faster than the 16MHz clock frequency of the Arduino. However, the input buffer to the Arduino should be filling up as Matlab is asynchronously transmitting data to it (especially if delays are used). Since I am not exceeding the 64 byte serial input buffer, I wouldn't expect this would be causing the issues. What else am I overlooking? Serial communication should be trivial between Matlab and the UNO.

Matlab

% Connect to Arduino the specified port
Port = '/dev/tty.usbmodem1451';
BaudRate = 9600;
DataBits = 8;

% Create the serial connection and open communication with the Arduino.
ser = serial(Port, 'BaudRate', BaudRate, 'DataBits', DataBits);
pause(0.5);
fopen(ser);

n = 3;
for i=1:n
    fprintf(ser, '%u', i);
    pause(.002);
end

display(ser.BytesAvailable);
A = fread(ser, n);
fclose(ser);

Arduino Sketch

void setup() {
  int baud = 9600;
  Serial.begin(baud);  
}

void loop() {
  long pause = 2; // Delay in milliseconds
  
  if(Serial.available() > 0) {
    byte b = Serial.read();
    
    // echo the data back to Matlab
    Serial.write(b);
    delay(pause);
  }
}

communication takes time, especially at 9600 baud, matlab should wait until the 3 bytes are send.
you could also try at 115200 baud.

second

delay(pause);

is not needed as you actively wait for serial available onthe arduino site

I understand that communication takes time. However, sending 3 bytes (24 bits) at a 9600 baud should only take 2.5 ms. Like I mentioned, I have tried delays ranging from 0ms - 2s. The transmission issues still occur. I have tried 115.2k, 57.6k, and 28.8k baud rates. The Arduino doesn't like any of these bauds, even when its connected to the serial monitor.

sending 3 bytes (24 bits) at a 9600 baud should only take 2.5 ms.

No 3 bytes take 3x11 bits = 33 bits: it includes a stop bit and a start bit and some time of silence

at 9600 baud it can send at MAXIMUM 9.6 bits per millisecond.

33 bits therefore take at MINIMUM 3.5 millisecond. However as the protocol is asynchronous and interrupt controlled it can take quite a bit longer.

(actual time is 3.12, as the Arduino sends 10 bits per byte, no overhead between bytes. see measurement below)

did a measurement: @ 9600 baud 3 bytes are actually send in 3120 microseconds.
That is equivalent to 10 bits, so the Arduino apparently does not use any time between bytes.

  Serial.begin(9600);
  Serial.println("Start ");

  while(Serial._tx_buffer_head != Serial._tx_buffer_tail); // wait until buffer empty
  start = micros();
  Serial.print("123");
  while(Serial._tx_buffer_head != Serial._tx_buffer_tail); // wait until buffer empty
  stop = micros();
  Serial.println();
  Serial.println(stop-start);