% -----------------------------------------------------------------------------
% LoadSimSectData.m
% -----------------------------------------------------------------------------
%
%  Loads a SimSect++ data output file into a struct from the indicated
% file, using the data type structure supplied as the second agument. 
% This function can be used to conveniently extract data from a raw 
% SimSect output and present it in a struct.
%
% -----------------------------------------------------------------------------
%
%  function data = LoadSimSectData( filebase, datatypes );
%
% -----------------------------------------------------------------------------
%  Input Arguments:
%    
%    filebase : string containing the base name of the data file.
%               If in the current directory, there is a file with the 
%               given base and extension .m, this procedure loads that 
%               file as a matlab binary file. Otherwise, it looks for 
%               a file with extension .data and attempts to import an 
%               ascii file.
%    datatypes : This argument is required to be a cell array of string 
%               integer pairs, which indicate the names of the fields of 
%               the data file and the number of columns they represent. 
%               The names are used in creating the fields in the output 
%               struct.
%
% -----------------------------------------------------------------------------
%  Outputs :
%
%    data : a struct containing fields corresponding to the data entries
%           specified in the datatypes input.
%
% -----------------------------------------------------------------------------
% $Id: LoadSimSectData.m,v 1.1.1.1 2003/10/28 20:05:59 ulucs Exp $
% -----------------------------------------------------------------------------

function data = LoadSimSectData( filebase, datatypes );

asciiname = strcat( filebase, '.data' );
matlabname = strcat( filebase, '.mat' );

if ( exist( matlabname ) == 2 )
%  disp(strcat('Loading MATLAB formatted file: ', matlabname) );
  load( matlabname );
  vars = vars';
elseif ( exist( asciiname ) == 2 )
%  disp(strcat('Loading ASCII formatted file: ', asciiname) );
  vars = load( asciiname );
else
  disp('Cannot find the requested file!');
  return;
end
  
j = 1;
data = [];
for ind = 1:length(datatypes);
  varname = datatypes{ind}{1};
  varsize = datatypes{ind}{2};
  data = setfield( data, varname, vars(:,j:j+varsize-1) );
  j = j + varsize;
end

