title: Matlab, fread, speed up reading file with multiple data types and multiple sample rates
body: <p>Using Matlab R2015a. I'm using <code>fread</code> to read sensor data from a binary file. The data contains multiple precisions. One signal is sampled at 5 times the rate of the other signals. The file is structured as a sequence of batches, where each batch has e.g. 1 sample of signal_A, but 5 samples of signal_B.</p>
  
  <p>I have googled for examples on how to load and format the data fast, but the solutions I have seen only represent cases where there is a single sampling rate, making the solution simpler, as far as I can tell.</p>
  
  <p>What I would like to avoid is the use of <code>for</code>-loops, which is quite slow. See below for an illustration of how the data is arranged in the file, and a simple code example of what I have now.</p>
  
  <p>Any suggestions on how to speed this up?</p>
  
  <p><a href="https://i.stack.imgur.com/nDsd3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nDsd3.png" alt="File structure"></a></p>
  
  <pre><code>clear;
  fid = fopen('binaryFile.bin','r');
  signal_B = [];
  
  numBatches = Inf; % read all
  batchSize = 17; % each batch takes up 17 bytes
  
  % PART 1 (everything that is not signal_B)
  batchSerialNumber = fread(fid, numBatches, '*uint32', batchSize-4);
  fseek(fid, 4, 'bof'); % rewind
  signal_A = fread(fid, numBatches, '*uint16', batchSize-2);
  fseek(fid, 6, 'bof'); % rewind
  misc_info = fread(fid, numBatches, '*uint8', batchSize-1);
  
  % PART 2 (signal_B)
  for i = 1:length(batchSerialNumber)
      fseek(fid, ((i-1)*batchSize) + 7, 'bof'); % set position in file, according to batch number (i)
      signal_B = [signal_B; fread(fid, 5, '*int16')]; % read the 5 samples of signal_B in this batch
  end
  </code></pre>