Interfacing Matlab with Arduino

Currently, I am working on a project in which I use Arduino Mega 2560(Clone) to regulate traffic lights in a four road junction. In this, I use Matlab to give time for which the green light should be active into Arduino.
I tried using below code, but the values that I send are not being read by Arduino. I tried this by using Arduino USB port(Serial Communication).

Matlab Code:

clc;
close all;
clear all;
Ton=[26,36,25,78];
z=4;
n=4;
b=serial('COM1','BaudRate',9600);
fopen(b);
for i=1:n
fprintf(b,'%s',char(Ton(i)));
end

fprintf(b,'%s',char(Z));
fclose(b);

Arduino Code:

#include <LiquidCrystal.h>
int green[] = {0,1,2,3},i,Ton[4],Pfac,L,P;
int red[] = {4,5,6,7};
LiquidCrystal lcd(14,15,18,19,20,21);

void setup() {
// put your setup code here, to run once:
for (i=0;i<4;i++){
pinMode(green*,OUTPUT);*
pinMode(red*,OUTPUT);*
}
for (i=0;i<4;i++){
_ digitalWrite(green*,LOW);
digitalWrite(red,LOW);
}
lcd.begin(16,2);
Serial.begin(9600);
}
void loop() {
if (Serial.available()>0){
Ton[0]=Serial.read();
Ton[1]=Serial.read();
Ton[2]=Serial.read();
Ton[3]=Serial.read();
Pfac=Serial.read();
}
digitalWrite(green[0],HIGH);
digitalWrite(red[1],HIGH);
digitalWrite(red[2],HIGH);
digitalWrite(red[3],HIGH);*_

L=Ton[0];
for (i=L;i>=0;i--){
* lcd.clear();*
lcd.print(i);
delay(1000);
}

digitalWrite(green[0],LOW);
* digitalWrite(red[1],LOW);*
* digitalWrite(red[2],LOW);*
* digitalWrite(red[3],LOW);*
* if (Pfac!=0){*
* interupt(Pfac);*
* }*
* if (Serial.available()>0){*
* Pfac=Serial.read();*
* }*

* digitalWrite(green[1],HIGH);*
* digitalWrite(red[2],HIGH);*
* digitalWrite(red[3],HIGH);*
* digitalWrite(red[0],HIGH);*
L=Ton[1];
for (i=L;i>=0;i--){
* lcd.clear();*
lcd.print(i);
delay(1000);
}
digitalWrite(green[1],LOW);
* digitalWrite(red[2],LOW);*
* digitalWrite(red[3],LOW);*
* digitalWrite(red[0],LOW);*
* if (Pfac!=0){*
* interupt(Pfac);*
* }*
* if (Serial.available()>0){*
* Pfac=Serial.read();*
* }*

* digitalWrite(green[2],HIGH);*
* digitalWrite(red[3],HIGH);*
* digitalWrite(red[0],HIGH);*
* digitalWrite(red[1],HIGH);*
L=Ton[2];
for (i=L;i>=0;i--){
* lcd.clear();*
lcd.print(i);
delay(1000);
}
digitalWrite(green[2],LOW);
* digitalWrite(red[3],LOW);*
* digitalWrite(red[0],LOW);*
* digitalWrite(red[1],LOW);*
* if (Pfac!=0){*
* interupt(Pfac);*
* }*
* if (Serial.available()>0){*
* Pfac=Serial.read();*
* }*

* digitalWrite(green[3],HIGH);*
* digitalWrite(red[0],HIGH);*
* digitalWrite(red[1],HIGH);*
* digitalWrite(red[2],HIGH);*
L=Ton[3];
for (i=L;i>=0;i--){
* lcd.clear();*
lcd.print(i);
delay(1000);
}
digitalWrite(green[3],LOW);
* digitalWrite(red[0],LOW);*
* digitalWrite(red[1],LOW);*
* digitalWrite(red[2],LOW);*
* if (Pfac!=0){*
* interupt(Pfac);*
* }*
* if (Serial.available()>0){*
* Pfac=Serial.read();*
* }*
* }*
int interupt(int Pfac){
* if (Pfac==1){*
* digitalWrite(green[0],HIGH);*
* digitalWrite(red[1],HIGH);*
* digitalWrite(red[2],HIGH);*
* digitalWrite(red[3],HIGH);*
L=Ton[0];
for (i=L;i>=0;i--){
* lcd.clear();*
lcd.print(i);
delay(1000);
}
digitalWrite(green[0],LOW);
* digitalWrite(red[1],LOW);*
* digitalWrite(red[2],LOW);*
* digitalWrite(red[3],LOW);*
* }*

* if (Pfac==2){*
* digitalWrite(green[1],HIGH);*
* digitalWrite(red[2],HIGH);*
* digitalWrite(red[3],HIGH);*
* digitalWrite(red[0],HIGH);*
L=Ton[1];
for (i=L;i>=0;i--){
* lcd.clear();*
lcd.print(i);
delay(1000);
}
digitalWrite(green[1],LOW);
* digitalWrite(red[2],LOW);*
* digitalWrite(red[3],LOW);*
* digitalWrite(red[0],LOW);*
* }*

* if (Pfac==3){*
* digitalWrite(green[2],HIGH);*
* digitalWrite(red[3],HIGH);*
* digitalWrite(red[0],HIGH);*
* digitalWrite(red[1],HIGH);*
L=Ton[2];
for (i=L;i>=0;i--){
* lcd.clear();*
lcd.print(i);
delay(1000);
}
digitalWrite(green[2],LOW);
* digitalWrite(red[3],LOW);*
* digitalWrite(red[0],LOW);*
* digitalWrite(red[1],LOW);*
* }*

* if (Pfac==4){*
* digitalWrite(green[3],HIGH);*
* digitalWrite(red[0],HIGH);*
* digitalWrite(red[1],HIGH);*
* digitalWrite(red[2],HIGH);*
L=Ton[3];
for (i=L;i>=0;i--){
* lcd.clear();*
lcd.print(i);
delay(1000);
}
digitalWrite(green[3],LOW);
* digitalWrite(red[0],LOW);*
* digitalWrite(red[1],LOW);*
* digitalWrite(red[2],LOW);*
* }*
* Pfac=0;*
}
I want to know where I did wrong, can anyone help me out...

Please read the post "How to use this forum" and follow the directions. Edit your post to add code tags.

FYI, The Mathworks (creators of Matlab/Simulink) are having a webinar today on using Matlab with Arduino. Read about it and register here: https://go2.mathworks.com/arduino-and-beyond-embedding-matlab-simulink-algorithms-on-hardware-lwb-na-1992374

I doubt if the presenter will be able to do a deep dive into your code but you might get some useful tips.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R