title: Using fixedRate timer in MATLAB with different data for each interval (trying to replace for loop with timer)
body: <p>Is there a way to have a MATLAB timer pass different data on each subsequent call to the timer function? My goal is to cycle through intervals at a fixed rate, and the pause function inside a loop is not precise enough.</p>
  
  <p>I have workng MATLAB code that uses a for loop to send data via serial ports, then wait a specified time before the next iteration of the loop. The serial communication varies in speed, so if I specify 300 seconds as the period, the loop actually executes every 340-360 seconds. Here is the existing code:</p>
  
  <pre><code>clear all;
  testFile = input('What is the name of the test data file (with extension): ', 's');
  measurementData = csvread(testFile);
  intervalDuration = input('What is the measurement change period (seconds): ');
  intervalNumber = size(measurementData,2);
  
  % Set up the COM PORT communication
  sensorComPort = [101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120];
  controllerComPort = [121,122,123,124];
  
  for j=1:intervalNumber
      tic
      fprintf('\nInterval # %2d\n',rem(j,24));
      sensorMeasurementPS = [measurementData(1,j),measurementData(2,j),measurementData(3,j),measurementData(4,j),measurementData(5,j), measurementData(6,j),measurementData(7,j),measurementData(8,j),measurementData(9,j),measurementData(10,j), measurementData(11,j),measurementData(12,j),measurementData(13,j),measurementData(14,j),measurementData(15,j), measurementData(16,j),measurementData(17,j),measurementData(18,j),measurementData(19,j),measurementData(20,j)];        
      serialSensorObj = startsensorPSWithoutReset(sensorComPort, sensorMeasurementPS);
      serialSensorObj = changeMeasurement(serialSensorObj, sensorMeasurementPS); 
      rc = stopsensorPS(serialSensorObj);
      controllerMeasurementPS = [measurementData(21,j),measurementData(22,j),measurementData(23,j),measurementData(24,j)];
      serialControllerObj = startControllerPSWithoutReset(controllerComPort, controllerMeasurementPS);
      serialControllerObj = changeMeasurement(serialControllerObj, controllerMeasurementPS);
      rc2 = stopControllerPS(serialControllerObj);
      pause(intervalDuration);
      t = toc;
      fprintf('Elapsed time = %3.4f\n',t);
  end
  
  clear serialSensorObj;
  clear serialControllerObj;
  </code></pre>
  
  <p>The serial functions are specified in other files and they are working as intended.</p>
  
  <p>What I need to do is have the serial communication execute on a more precise 5-minute interval. (The actual timing of the commands inside the interval will still vary slightly, but the commands will kick off every 5 minutes over the course of 24 hours. The current version loses time and gets out of sync with another system that is reading the measurements I'm setting by serial port.)</p>
  
  <p>My first thought is to use a MATLAB timer with the fixedRate execution mode, which queues the function at fixed intervals. However, it doesn't appear that I can send the timer function different data for each interval. I thought about having the timer function change a counter in the workspace, similar to j in my existing for loop, but I know that having functions interact with the workspace is not recommended.</p>
  
  <p>Here's what I've come up with so far for the timer method:</p>
  
  <pre><code>function [nextJ] = changeMeasurement_fcn(obj,event,j,sensorComPort,controllerComPort)
  
  tic
  fprintf('\nInterval # %2d\n',rem(j,24));
  sensorMeasurementPS = [measurementData(1,j),measurementData(2,j),measurementData(3,j),measurementData(4,j),measurementData(5,j), measurementData(6,j),measurementData(7,j),measurementData(8,j),measurementData(9,j),measurementData(10,j), measurementData(11,j),measurementData(12,j),measurementData(13,j),measurementData(14,j),measurementData(15,j), measurementData(16,j),measurementData(17,j),measurementData(18,j),measurementData(19,j),measurementData(20,j)];        
  serialSensorObj = startSensorPSWithoutReset(sensorComPort, sensorMeasurementPS);
  serialSensorObj = changeMeasurement(serialSensorObj, sensorMeasurementPS); 
  rc = stopSensorPS(serialSensorObj);
  controllerMeasurementPS = [measurementData(21,j),measurementData(22,j),measurementData(23,j),measurementData(24,j)];
  serialControllerObj = startControllerPSWithoutReset(controllerComPort, controllerMeasurementPS);
  serialControllerObj = changeMeasurement(serialControllerObj, controllerMeasurementPS);
  rc2 = stopControllerPS(serialControllerObj);
  t2 = toc;
  fprintf('Elapsed time = %3.4f\n',t2);
  </code></pre>
  
  <p>and this is how I would call it from the main m file:</p>
  
  <pre><code>t = timer('TimerFcn',@changeMeasurement,'ExecutionMode','fixedRate','period',intervalDuration);
  % then I need some code to accept the returned nextJ from the timer function
  </code></pre>
  
  <p>This feels a bit sloppy so I'm hoping there's a built-in way to have a timer cycle through a data set.</p>
  
  <p>Another idea I had was to keep the for loop but change the pause function to use a value calculated based on how much time would add up to 5 minutes for the iteration.</p>
  
  <p>To summarize my question:
      a) Can I have a timer pass different data to the timer function on each iteration?
      b) Is that a good way to go about cycling through the intervals in my data on a precise 5-minute interval?</p>
  
  <p>Thanks!</p>