% Create a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM6', 'Tag', '');

% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
    obj1 = serial('COM6');
else
    fclose(obj1);
    obj1 = obj1(1);
end

% Set the BaudRate
obj1.BaudRate = 4800;

% Connect to instrument object, obj1.
fopen(obj1);

for i=1:100
    
    % We only want the strings with $GPRMC…
    data='';
    while isempty(strmatch('$GPRMC',data))
        data = fscanf(obj1);
        %   disp('-');
    end
    
    %data = '$GPRMC,160200.000,A,3317.3849,S,01751.3850,E,6.89,336.61,050910,,*1C';
    
    % Parse the string to obtain coordinates
    [frame,data] = strtok(data,',');
    [time,data] = strtok(data,',');
    [letter,data] = strtok(data,',');
    [lat,data] = strtok(data,',');
    [nsCardinal,data] = strtok(data,',');
    [long,data] = strtok(data,',');
    [ewCardinal,data] = strtok(data,',');
    [letter,data] = strtok(data,',');
    [letter,data] = strtok(data,',');
    [date,data] = strtok(data,',');
    %lat = str2double(lat);
    %long = str2double(long);
    
    % Display the results
    %disp({time,date,lat,nsCardinal,long,ewCardinal});
    disp([num2str(time) ',' num2str(date) ',' num2str(lat) ',' nsCardinal ',' num2str(long) ',' ewCardinal]);
    
    %   pause(1);
    
end


% Free the serial port
fclose(obj1);