function dataout = LAB3DATACONV(filename)

%% [time data] = LAB3DATACONV(filename)
%
% LAB3DATACONV(filename) converts the data of 'filename' into a more readable
% time format. 'filename' is a tabbed/spreadsheet file created by LabView.
%
% The function returns a matrix of [time data] where 'time' is the offsetted
% value of each data piece recorded. It uses the first piece of recorded 
% data as the base for the offset value. 'data' is the recorded data taken
% at that particular time.
%
% Author R. Agrawal
% October 2000
%% 

fid = fopen (filename);
% Counts the number of rows in the file
numrows = 1;
while not (feof (fid))
	fgetl(fid);
   numrows = numrows + 1;
end;

% Reopens the fcle to set the reader at the beginning of the file
fid = fopen (filename);

headerline = fgetl(fid);
columnline = fgetl(fid);

% Grabs the first piece of data to set the offsetvalues
firstdate = fgets(fid,10);
houroffset = fscanf(fid, '%d', 1);
dummyvar = fscanf(fid, '%c', 1);
minuteoffset = fscanf(fid, '%d', 1);
dummyvar = fscanf(fid, '%c', 1);
secoffset = fscanf(fid, '%d', 1);
dummyvar = fscanf(fid, '%c', 1);
millisecoffset = fscanf(fid, '%d', 1);
dummyvar = fscanf(fid, '%c', 1);
data1 = fscanf(fid, '%11f', 1);
dummyvar = fgetl (fid);

% Creates the new matrix for the new data
dataout = zeros(numrows-2, 2);
row = 1;

% Sets the data for the first row
dataout(row, 1) = 0;
dataout (row, 2) = data1;

timeoffset = millisecoffset/10 + secoffset*1000 + minuteoffset*1000*60 + houroffset*1000*60*60;

while not (feof(fid))
   row = row + 1;
   
   % Grabs the data for each row
   datadate = fgets(fid,10);
	hour = fscanf(fid, '%d', 1);
	dummyvar = fscanf(fid, '%c', 1);
	minute = fscanf(fid, '%d', 1);
	dummyvar = fscanf(fid, '%c', 1);
	second = fscanf(fid, '%d', 1);
	dummyvar = fscanf(fid, '%c', 1);
	millisec = fscanf(fid, '%d', 1);
   newdata = fscanf(fid, '%11f' ,1);;
   dummyvar = fgetl(fid);
   
   % Calculates the offsetted value for milliseconds
   if millisec >= millisecoffset
      newmilli = millisec - millisecoffset;
      elseif second ~= 0
      	millisec = millisec + 10000;
	      newmilli = millisec - millisecoffset;
   	   second = second - 1;
      elseif minute ~= 0
         second = second + 59;
         millisec = millisec + 10000;
         newmilli = millisec - millisecoffset;
         minute = minute - 1;
      elseif hour ~= 1
         minute = minute + 59;
         second = second + 59;
         millisec = millisec + 10000;
         newmilli = millisec - millisecoffset;
         hour = hour - 1;
      else
         hour = 12;
         minute = minute + 59;
         second = second + 59;
         millisec = millisec + 10000;
         newmilli = millisec - millisecoffset;      
   end
   
   % Calculates the offsetted value for seconds
	if second >= secoffset
      newsec = second - secoffset;
      elseif minute ~= 0
         second = second + 60;
         newsec = second - secoffset;
         minute = minute - 1;
      elseif hour ~= 1
         minute = minute + 59;
         second = second + 60;
         newsec = second - secoffset;
         hour = hour - 1;
      else
         hour = 12;
         minute = minute + 59;
         second = second + 60;
         newsec = second - secoffset;      
   end

   % Calculates the offsetted value for minuteuteutes
	if minute >= minuteoffset
      newminute = minute - minuteoffset;
      elseif hour ~= 1
         minute = minute + 60;
         newminute = minute - minuteoffset;
         hour = hour - 1;
      else
         hour = 12;
         minute = minute + 60;
         newminute = minute - minuteoffset;      
   end
     
   % Calculates the offsetted value for hours
   if hour >= houroffset
      newhour = hour - houroffset;
      else
         hour = 12;
   end
      
   % Converts the new offsetted time values into milliseconds
   newtime = newmilli/10 + newsec*1000 + newminute*60*1000 + newhour*60*60*1000;
   
   % Appends the time and the new data to the matrix
	dataout(row, 1) = newtime;
   dataout(row, 2) = newdata;
end;

% Formats the environment to display all decimal places for the data and the time
format long e;

fclose(fid);