How to send an array to arduino from matlab?

I am trying to send an array of integers to arduino from matlab through serial communication but I feel my arduino is not receiving the data ass i am not getting the desired response can anyone please guide me as to what is the error in my program.
My code
void loop() {
// put your main code here, to run repeatedly:
int i;
if(Serial.available()>0)
{
for(i=0;i<4;i++)
{
k*=Serial.read();*

  • }*
    }
    a=k[3];
    digitalWrite(a,HIGH);
    delay(20000);
    I urgently need help.

Presumably k is declared as an array above setup() and you forgot to copy that part?

And not that it’s anything to do with your problem, did you not notice that your post went all italic on you? That’s the i in square brackets that has disappeared because the browser thought it meant turn italics on.

So use the code tag icon </> top left to get the code in a box with no italics.

You’re trying to do this:

k[i]=Serial.read();

… a fixed number of times in the for() loop but you don’t know how many characters are available to serial, only that there’s at least 1 (ie, >0). It might be 1, might be a gazillion…

I would try: lose the for() loop, and let loop() do the looping (it’s good at that ;), clue’s in the name ) and increment i manually.

And thinking about it a bit more, what's the format of the data in matlab? Is it integers separated by commas or what? Or integers separated by time?

You could perhaps use Serial.parseInt()....

I tested this:

int k[20];
byte i = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("setup done");
}//setup

void loop()
{
  if (Serial.available() > 0)
  {
    k[i] = Serial.parseInt();
    Serial.print("k["); Serial.print(i); Serial.print("] = "); Serial.println(k[i]);
    i++;
  }
}//loop

Typed 123 456 789 (with spaces) in the serial monitor input line, and got:

setup done
k[0] = 123
k[1] = 456
k[2] = 789

(edit: and since parseInt() parses ints out of any old cr@p, it works the same when I typed 123;456#789)

But for more help you'll need to give more details about what's coming from Matlab.

And also see Robin2's serial tutorial for good robust ways to get stuff from serial.