%% Extract Data from NMEA Sentences Using MessageID Property
% Extract data from any of the nine standard NMEA sentences as part of the built-in support using the MessageID property. The NMEA data is obtained from a GNSS receiver.
%% Extract Data from RMC Sentence
% Create an nmeaParser System Object by specifying the Message ID as "RMC".

pnmea = nmeaParser("MessageID","RMC");

% Provide the RMC sentence obtained from the GNSS receiver as the input and extract data.

unparsedRMCLine='$GNRMC,143909.00,A,5107.0020216,N,11402.3294835,W,0.036,348.3,210307,0.0,E,A*31';
rmcData = pnmea(unparsedRMCLine)

%
%% Extract Data from Multiple NMEA Message Types
% Provide GGA, GSA, and RMC sentences as the input.

unparsedGGALine = ['$GPGGA,111357.771,5231.364,N,01324.240,E,1,12,1.0,0.0,M,0.0,M,,*69'];
unparsedGSALine = ['$GPGSA,A,3,01,02,03,04,05,06,07,08,09,10,11,12,1.0,1.0,1.0*30'];
unparsedRMCLine = ['$GPRMC,111357.771,A,5231.364,N,01324.240,E,10903,221.5,020620,000.0,W*44'];

% Create a string array to include the three sentences

rawNMEAData = [unparsedGGALine ,newline,  unparsedGSALine ,newline,  unparsedRMCLine]

% However, consider that you need to extract data only from GGA and GSA sentences. So create the nmeaParser System Object 'pnmea', and specify the 'GGA' and 'GSA' Message IDs as a string array.

pnmea=nmeaParser("MessageIDs",["GGA","GSA"]); 

% Specify the output arguments for all the three sentences to extract the data as structures.

[ggaData,gsaData] =  pnmea(rawNMEAData)

% The above output shows that only GGA and GSA sentences are extracted based on the Message IDs specified as input.
% Provide another GGA sentence as an additional input, and extract data. In this case, you need not modify the System object as the Message ID has not changed.

unparsedGGALine1='$GNGGA,001043.00,4404.14036,N,12118.85961,W,1,12,0.98,1113.0,M,-21.3,M,,*47'
rawNMEAData = [unparsedGGALine ,newline,  unparsedGSALine ,newline,  unparsedGGALine1]
[ggaData,gsaData] =  pnmea(rawNMEAData)

% A status of 0 indicates that the data was parsed successfully.
%
%% Extract Data from GSV Sentence
% Create an nmeaParser System Object by specifying the Message ID as "GSV".

pnmea = nmeaParser("MessageID","GSV");

% Provide the GSV sentence obtained from the GNSS receiver as the input and extract data.

unparsedGSVLine='$GPGSV,3,3,10,32,69,205,41,46,47,215,39*79';
gsvData = pnmea(unparsedGSVLine)

%
%% Extract Data from Multiple GSV Sentences
% Provide multiple GSV sentences as the input.

unparsedGSVLine1 = '$GPGSV,3,1,10,01,,,31,03,28,325,40,10,,,33,12,20,047,30*70';
unparsedGSVLine2 = '$GPGSV,3,2,10,14,88,028,42,22,39,299,48,25,,,25,31,79,289,46*49';
unparsedGSVLine3 = '$GPGSV,3,3,10,32,69,205,41,46,47,215,39*79';

% Create a string array to include the three sentences.

CRLF = [char(13),newline];
unparsedGSVLines = [unparsedGSVLine1,CRLF, unparsedGSVLine2, CRLF, unparsedGSVLine3];

% Create the nmeaParser System Object 'pnmea', specify the 'GSV' Message ID, and extract data.

pnmea = nmeaParser("MessageIDs","GSV");
gsvData = pnmea(unparsedGSVLines)

%
%% Read Data from NMEA Log
% Read data from a sample NMEA log, so that the data can be parsed using the nmeaParser System Object.
% The sample log file is nmeaLog.nmea, which is included in this example.

f = fopen('nmeaLog.nmea');
unParsedNMEAdata = fread(f);
pnmea = nmeaParser("MessageIDs",["RMC","GGA"]);
[rmcStruct, ggaStruct] = pnmea(unParsedNMEAdata)

% Copyright 2020 The MathWorks, Inc.