% readgpgsv.m
% Drew Gashler
% 06 February 2001
% 
% Reads archived gpgsv NMEA string from gps and loads into matlab variables.
% Assumes that incomplete NMEA sentences have been removed from the data file.
% function y = readgpgsv(filename);


function y = readgpgsv(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 = ',';

% remove the while loop temporarily
%while(1)
% put in the for loop to limit the number of samples if desired.
for i = 1:5000
    string_buffer = fgets(fid);             % read in a line from the file

    if ~isstr(string_buffer),break,end      % Tests for the end of the file

    delimiters = findstr(string_buffer,delimiter);  % Find the locations of the comma delimiters.
    for j = 1:12
      wordNumber = j + 3;
      dataString = string_buffer(delimiters(wordNumber)+1:delimiters(wordNumber+1)-1);
      if (~isempty(dataString))
         data(i,j) = str2num(dataString);
      else data(i,j) = NaN;
      end
    end 
    i           = i + 1;
end

% sort the data by satellite PRN
sortedData = sortrows(data,1);
% find transitions between satellite PRN
%whosbreaks = find(diff(sortedData,:,1));
y = sortedData;

fclose(fid);






