function [data,error] = loadeng(filename,framelength,frames)
% LOADENG  load Unsteady Aero .eng files
%
% [data,error] = loadeng(filename,framelength,frames)
%
%          filename is the name of the file to be loaded
%          framelength is the number of channels per frame
%          frames is the number of frames to read
%
%          example usage:
%
%            data=loadeng('filename.txt',285,15625);
%
%          loads 15625 samples of 285 channels from the file 'filename.txt'
%          into a matrix called data
%
%          These numbers can be found in the second line of the
%          corresponding .hd1 file.  The third number is the sample period:
%
%          UAE-P3 HD1 FILE
%          285,15625,0.001920
%
%          The .hd1 file also contains the channel names, in order, as well
%          as statistics about the data set which are also in the summary
%          spreadsheets.
%
%          See the Phase VI report for channel descriptions.
%
%          error (if used) will contain integers representing the error byte

% Open the file in read only using little endian format
fid=fopen(filename,'r','l');

if(fid~=-1)
  data=zeros(frames,framelength);
  e=zeros(frames,1);
  for i=1:frames,
    % Read data
    [d,c]=fread(fid,framelength,'float32');
    data(i,:)=d';

    % Read error byte
    [d,c]=fread(fid,1,'uint8');
    e(i)=d;
  end; % for i
  fclose(fid);
end; % if

if(nargout==2) error=e;end;
