%% Extract Data Using CustomSentence Name-Value Pair
% You can extract data from NMEA sentences using the CustomSentence name-value pair. The NMEA data to be parsed is obtained from marine electronic devices.
%% Identify the Structure of NMEA Sentence and Create the Parser Function
% You need to identify the structure of the NMEA sentence, as defined in the specification, and use that information to define the structure of output data to be used in the nmeaParser System object.
% For example, let us consider an example sentence with Message ID, SSS.
%
% After identifying the structure, you create a function file that defines the parser function, fsssParser. In the function file, you define the output data as a structure array with its fields matching the sequence as it appears in the specification.
% The Navigation Toolbox™ provides an optional pre-configured function, extractNMEASentence, that checks if the sentence is valid and convert the fields in the sentence into string array. You can call  extractNMEASentence inside the function file. You can also use any other function instead (which outputs a string array from unparsed data), and then call it inside the function file.
% The below image shows the function file with the code, with the assumption that the fields available in SSS sentence are TalkerID, MessageID, UTC, and LatitudeError. Refer the additional comments for details.
%
% In the above sample file, you define the mapping between the fields in the structure array and the elements of string array. For certain fields (for example, UTC time), you may need to define a character array to map the fields.
% After you include the MATLAB code as mentioned above, save the function file (fsssParser.m) in the MATLAB path, so that you can call it to obtain parsed data using the CustomSentence name-value pair of nmeaParser System object.
% To download another sample function file parserRMB.m that is used this example, click Open Live Script. This is a function file specific to the fields of an RMB sentence (mentioned in NMEA Standard, Version 4.1).
%% Extract Data from RMB Sentence
% Create an nmeaParser System Object by using the CustomSentence name-value pair and specifying the message ID as "RMB" and the function as "parserRMB" (downloaded in the previous step).

pnmea = nmeaParser("CustomSentence",{["RMB","parserRMB"]});

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

unparsedRMBLine='$GPRMB,A,4.08,L,EGLL,EGLM,5130.02,N,00046.34,W,004.6,213.9,122.9,A*3D';
rmbData = pnmea(unparsedRMBLine)

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

unparsedRMBLine1 = ['$GPRMB,A,0.66,L,003,004,4917.24,N,12309.57,W,001.3,052.5,000.5,V*20'];
unparsedRMBLine2 = ['$GPRMB,A,4.08,L,EGLL,EGLM,5130.02,N,00046.34,W,004.6,213.9,122.9,A*3D'];

% Create a character array to include the two sentences

rawNMEAData = [unparsedRMBLine1 ,newline,  unparsedRMBLine2]

% Specify the output argument for the RMB sentence to extract the data.

[rmbData] =  pnmea(rawNMEAData)

%% Extract Data from a Sentence with Built-in Support (RMC) and RMB Sentence
% Create an nmeaParser System Object by using the MessageID property (to parse a sentence with built-in support - RMC) and also using the CustomSentence name-value pair (specifying the message ID as "RMB" and the function as "parserRMB" (created in a previous step)).

pnmea = nmeaParser("MessageID","RMC","CustomSentence",{["RMB","parserRMB"]});

% Provide RMC and RMB sentences as the input.

unparsedRMCLine1 = ['$GNRMC,143909.00,A,5107.0020216,N,11402.3294835,W,0.036,348.3,210307,0.0,E,A*31'];
unparsedRMBLine2 = ['$GPRMB,A,4.08,L,EGLL,EGLM,5130.02,N,00046.34,W,004.6,213.9,122.9,A*3D'];

% Create a string array to include the two sentences

rawNMEAData = [unparsedRMCLine1 ,newline,  unparsedRMBLine2]

% Specify the output argument for the RMB sentence to extract the data.

[rmcdata,rmbData] =  pnmea(rawNMEAData)

% Copyright 2021 The MathWorks, Inc.
%