% Generic data load from file

function data = load_data(filename,num_components)

record_size = 8+num_components*8; %time plus N states
size_of_double = 8;

fid = fopen(filename,'rb');

if fid == -1
  [~, missingFile, ext] = fileparts( filename );
  warning('Failed to open file %s%s.', missingFile, ext ) %#ok<WNTAG>
  data = 0;
  return ;
end

%load data as a 2-D array

fseek(fid,0,'eof');
file_len = ftell(fid);
fseek(fid,0,'bof');
num_records = file_len / record_size;

[data, count] = fread(fid,[num_components + 1 num_records],'double');

data = data';  %reshape to have a column for each measurement and a row for each epoch
fclose(fid);
