Sensor data from Arduino to Matlab

hey guys

im doing a project in which i scan a 15x15 grid using arduino. im trying to send the data in a line of 225 numbers than read that in using matlab, turn it into a matrix and make a 3d plot of the grid.

most of the time it will display fine but if i manipulate the sensor to change the data on too many points of the grid it will fail and not get enough points at the fscanf function in matlab (it always has 225 data points when i view the serial monitor through arduino).

I think its just a problem with how I am reading the data in matlab (maybe a syncing issue?)

any help would be appreciated
thanks!

arduino code:

int Signal[] = {
  A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14};
int output[] = {
  24,26,28,30,32,34,36,38,40,42,44,46,48,50,52};
int SenselReading[14];  //set an empty 15 point array

void setup() 
{

  Serial.begin(9600);
  for (int i=0; i<=14 ;i++)
  {
    pinMode(Signal[i],INPUT); // set the analog input pins A0~A5 as the inputs
    pinMode(output[i], OUTPUT); // set the digital pins 8,9,10 as the outputs
  }
}

void loop() 
{
  for (int i = 0; i <=14; i++)
  {
    digitalWrite(output[i], HIGH);

   // delay(10);
    for (int j = 0; j<=14; j++)
    {
      if (analogRead(Signal[j]) <= 10)
      {
      SenselReading[j] = 1;
      }
      
      else{
      SenselReading[j] = analogRead(Signal[j]);
      }
      
      if (i==14 && j==14)
      {
        
        Serial.println(SenselReading[j],DEC);
        
      }
      else{
        Serial.print(SenselReading[j],DEC);
        Serial.print("\t");
      }

      delay(3);

    }
    digitalWrite(output[i], LOW);
  }
}

matlab code:

clc;
clear all;
delete(instrfindall)
s = serial('/dev/tty.usbmodem1421'); % define the 's' as the Arduino port

set(s,'BaudRate',9600);  % set the frequence of the signal
set(s,'DataBits',8);     % set the data bits
set(s,'Parity','none');
out=zeros(1,225);


fopen(s);    % open the Arduino port and make it accessable to get data from Arduino to Matlab
% i = 0;
pause(1);
while 1      % keep moving all the way and get data in real-time
    try

        line = fscanf(s)   % get data if there exists data in the next line
        length(line)
    
    catch
        fclose(s);         % close it when accident happens
    end
    

    try
        out = strread(line); % read the data from the 'line'
        length(out)
    catch
        fclose(s); 
    end
    

%this block turns it into a matrix and interpolates, then plots the data
    hold on
       if length(out) == 225
        A = [out(1:15); out(16:30); out(31:45);out(46:60);out(61:75);...
            out(76:90);out(91:105);out(106:120);out(121:135);out(136:150);...
            out(151:165);out(166:180);out(181:195);out(196:210);out(211:225)];
        [X,Y] = meshgrid(1:1:15,1:1:15);
        [Xq,Yq] = meshgrid(1:.25:15);
        Vq = interp2(X,Y,A,Xq,Yq,'cubic');
        surf(Xq,Yq,Vq)
      end
    
    hold on

    drawnow
    
    box on
    grid on
    axis on
    caxis([-50 500])
    colorbar;
    axis([1 15 1 15 -10 500]);

    cla
    
end
int Signal[] = {
  A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14};

Perhaps you need to use long or long long.

    pinMode(Signal[i],INPUT); // set the analog input pins A0~A5 as the inputs
    pinMode(output[i], OUTPUT); // set the digital pins 8,9,10 as the outputs

If you are not going to make the comments correct, get rid of them.

int SenselReading[14];  //set an empty 15 point array

Guess again!

      if (i==14 && j==14)
      {
        
        Serial.println(SenselReading[j],DEC);
        
      }
      else{
        Serial.print(SenselReading[j],DEC);
        Serial.print("\t");
      }

It would be a lot simpler to send the value and a tab, and then send the carriage return and line feed after the two loops end. Yes, it would send one extra tab, but is that really a problem?

      delay(3);

Why?

it always has 225 data points when i view the serial monitor through arduino

You don't seem to understand that serial data delivery is not guaranteed.

Why does the sensor data need to be stored in an array (that is too small) anyway? You never use the array data, except for sending the last element stored. That doesn't need an array.

Hey Paul thanks for your response.

Yes I should have changed or deleted the comments, and yea there are many things I don't understand yet haha...

I did find a solution though if anyone else is looking for help here too:

my InputBufferSize was too small so in MatLab I put this code in:

s = serial('/dev/tty.usbmodem1421'); % define the 's' as the Arduino port
set(s, 'InputBufferSize', 1024);

the default is 512 and I was surpassing that if I created numbers too large in my grid (probably could have been fixed by not using an array but it works haha).

basically i m doing a project to find distance using ultrasonic sensor i have done it with arduino but i want the distance calculated value in matlab ..plzzzzzz help me....i m stuckon this for a week....plzz help me..how to read the value from arduino to matlab

check that and u can send your task here and they will help u http://www.maklabacademy.com/start-with-matlab-and-arduino/

Any info on how to use a basic Mx2125 accelerometer setup/code and translate it to matlab