function q = read_motionfile2(fname)
% Purpose:  This function reads a file in the format of a SIMM motion file
%           and returns a data structure
%
% Input:    fname is the name of the ascii datafile to be read ('character array') 
%
% Output:   q returns a structure with the following format:
%				q.labels 	= array of column labels
%				q.data 		= matrix of data
%				q.nr 		= number of matrix rows
%				q.nc 		= number of matrix columns
%
% ASA 12/03


% Open ascii data file for reading.
fid = fopen(fname, 'r');	

% Check for file error.
% If error, send message to screen and stop execution.
if fid == -1								
	error(['unable to open ', fname])		
end

% Process the file header.
nextline = fgetl(fid);	
while strncmpi(nextline, 'endheader', length('endheader')) ~= 1
	if strncmpi(nextline, 'datacolumns', length('datacolumns')) == 1
		q.nc = str2num(nextline(findstr(nextline, ' ')+1 : length(nextline)));
	elseif strncmpi(nextline, 'datarows', length('datarows')) == 1
		q.nr = str2num(nextline(findstr(nextline, ' ')+1 : length(nextline)));	
	end
	nextline = fgetl(fid);
end

% Process the column labels.
% nextline = fgetl(fid);			% read blank line before column labels
nextline = fgetl(fid);			% read line containing column labels
q.labels = cell(1, q.nc);
for j = 1:q.nc
	[q.labels{j}, nextline] = strtok(nextline);
end

% Process the data.
% Note:  transpose is needed since fscanf fills columns before rows.
q.data = fscanf(fid, '%f', [q.nc, q.nr])';

fclose(fid);


	









