Matlab and Arduino Serial communication not working

Hi everyone I'm having lots of trouble sending a value from Matlab to the Arduino Uno through the serial port. I know Matlab is sending the value over and Arduino is receiving it but I don't understand how to get Arduino to read the value. I have tried so many different things and nothing has worked for me I've searched all over the internet but I'm still having trouble.

So my Matlab code below just sends the number 4 to the serial port.

priorPorts = instrfind; % finds any existing Serial Ports in MATLAB
delete(priorPorts); % and deletes them

clear all
clc

a=serial('COM3');
set(a,'BaudRate',115200,'DataBits',8,'FlowControl','none');
set(a,'Parity','none');
set(a,'StopBits',1,'Terminator','CR');
set(a,'OutputBufferSize',512); 
set(a, 'Timeout', 10);
set(a,'InputBufferSize',512);

fopen(a);
  
%fprintf(a,'4');
fwrite(a,'4 ');

fclose(a);
delete(a);
clear a;

So I'm confused as to how to get Arduino to read the value. Does Arduino read an integer in its ascii format? Either way no matter what I do Arduino doesn't read the value, if I run the code I have below all I get is "-1" which I think means there's nothing there. I have also tried using Serial.parseInt() also to read an integer but that didn't work. The arduino code below simply reads the serial port and prints what it read but so far I can't get it to read anything.

int matlabData=0;
void setup() 
{  
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT);
  
  Serial.begin(115200);
  delay(4000);
  
    
} 

void loop(){ 
// Serial.flush();   
  
  if (Serial.available()>0) // if there is data to read
  delay(5);
   {
   matlabData=Serial.read(); //read data
   Serial.println(matlabData);
          
  
        }
  
  }

If anyone can help me with this in anyway I would greatly appreciate it. In the end I need Arduino to read the value and be put into a function such as running a motor a certain amount of steps. Thanks for any help in advance :).

fopen(a);
  
%fprintf(a,'4');
fwrite(a,'4 ');

fclose(a);

Open the serial port, resetting the Arduino. Before it can complete resetting, jam some data at it, and then close the serial port, causing the Arduino to reset. I don't think this was mentioned on the "best practices" page, was it?

I know Matlab is sending the value over

Yes.

and Arduino is receiving it

You know this how?

but I don't understand how to get Arduino to read the value.

Perhaps there was a clue above.

The Arduino has an led that blinks when it receives data so it has to be receiving something. Hm okay I didn't know that doing that resets the Arduino. So you're saying I should pause the Matlab code before and after I do the fwrite? I'll try a few things with that information thanks for the help.

I’m still confused as to how I can even see what Arduino is receiving. If I am doing Serial.println(matlabData) is it supposed to show me what it received from the serial port or am I already printing it after the data has passed so that’s why its only showing
“-1’s”?

If I am doing Serial.println(matlabData) is it supposed to show me what it received from the serial port

There are too many pronouns (it) without referents (what "it" is) to make sense of that question.

  if (Serial.available()>0) // if there is data to read
  delay(5);
   {
   matlabData=Serial.read(); //read data
   Serial.println(matlabData);
        }

If there is serial data to be read, delay for 5 milliseconds for no apparent reason.

Then, whether there is data, or not, read from the serial port. That is just wrong.