title: Real Time Data Import from Arduino to Matlab. What BufferSize? Why delay?
body: <p>I am using a Kitronyx Snowboard (it has an Arduino Leonardo) with the Matrix Sensor 1610. I want to read my data from the serial port directly into Matlab and visualize it. </p>
  
  <p>I have two problems which I think are related:
  First of all I can read the Matrix but with a delay. The longer I wait the longer the delay.</p>
  
  <p>Second of all after a while I’ll always get:</p>
  
  <p>Error using serial/fprintf (line 144)</p>
  
  <p>Unexpected error: A timeout occurred during the write operation..</p>
  
  <p>The bigger the InputBufferSize the longer I can plot my data (which doesn’t really matter because of problem one)</p>
  
  <p>How do I know what’s the convenient buffer size? Has anyone any idea why the delay and how I can get rid of it?</p>
  
  <p>Thank you so much in advance</p>
  
  <p>Mike</p>
  
  <pre><code>close all                   %close all figures
  clear all                   %clear all workspace variables
  fclose('all')               %close all Files
  delete(instrfindall)        %Reset Com Port
  delete(timerfindall)        %Delete Timers
  
  % setup serial
  
  serialPort = serial('COM3');
  
  command = 'A';
  
  nrow = 16;
  ncol = 10;
  row_index = [9,10,11,12,13,14,15,16,8,7,6,5,4,3,2,1];
  col_index = [1,2,3,4,5,6,7,8,9,10];
  
  % 10x16 = 160 bytes
  lendata = 160;
  BaudRate = 11520;
  InputBufferSize = 115200;
  Timeout = 5;
  set(serialPort , 'BaudRate', BaudRate);
  set(serialPort , 'InputBufferSize', InputBufferSize);
  set(serialPort , 'Timeout', Timeout);
  
  fopen(serialPort);
  
  while 1
  
  % Request data
  fprintf(serialPort, command);
  
  % Get data
  %Data is read as string (CSV)
  data_string = fgets(serialPort);
  data_string_array = strsplit(data_string, ',');
  data = str2double(data_string_array);
  
  % Reshape data (1D -&gt; 2D array)
  data2d = zeros(nrow, ncol);
  k = 1;
  for i = 1:nrow
     for j = 1:ncol
        data2d(row_index(i), col_index(j)) = data(k);
        k = k + 1;
     end
  end
  
  %resize 16x10 image to 160x100 image
  data2d_resized = imresize(data2d,50);
  imshow(data2d_resized,[0 100]);
  
  end
  </code></pre>