% readgga.m
% Drew Gashler
% 06 February 2001
% 
% Reads archived gga NMEA string from gps and loads lat,lon, into matlab variables.
% Assumes that incomplete NMEA sentences have been removed from the data file.

function [lat, lon] = readgga(filename);
% open the file:
fid = fopen(filename,'rt');

line = 'junk';
% read and skip incomplete NMEA sentences:
%while ((line(1) ~= '$')|(isempty(line) == 1))
%  line = fgetl(fid);
%end	



%initialize the row number
i = 1;
string_buffer = 'initial value';
% define the delimiter:
delimiter = ',';
numWords = 15;
% remove the while loop temporarily
while(1)
  string_buffer = fgets(fid);
  if ~isstr(string_buffer),break,end
  latPrime(i)    = sscanf(string_buffer(15:24),'%f');
  lonPrime(i)        = sscanf(string_buffer(27:37),'%f');
  i           = i + 1;
end

% latitude in the form 3648.1234, need to convert
% integer part of solution:
latInt = floor(latPrime/100);
lonInt = floor(lonPrime/100);

% decimal minutes part of solution
latDec = 100*(latPrime./100-latInt)./60;
lonDec = 100*(lonPrime./100-lonInt)./60;

% sum to get the total
lat = latInt + latDec;
lon = lonInt + lonDec;

%global lat lon latInt latDec lonInt lonDec latPrime lonPrime

fclose(fid);







