How to extract lat and long and height from a $GPGGA string and time match to another file?

10 views (last 30 days)
Hello! I wanted to ask for some help extracting my positional information. I have two datasets one is positioning and ellipsoidal height in $GPGGA strings and the other is my echosounder data, both of these have time stamps that should match up close enough. What I am currently trying to do (after cleaning and plotting my data)is to link the two together in a master file, but before I can do that I need to extract the latitude and longitude and the ellipsoidal height. I have tried to use textscan but eith no luck. Furthermore, the lines that don't contain the positional info I have to keep blank or as a NaN because otherwise the position would be shiften to a different time which would make it wrong. I hope this makes sense here is an example of my data array. I copied 20 lines as an example. I need to extract each line that starts with $GPGGA and then the value after N and the value after W as well as the value between two M letters. I bolded the data I need to extract. Its all in one line no columns. The lines that start with anything else than $GPGGA I need to keep as a nan. I have tried textscan and multiple other things but can't seem to figure out how to do this step. I think it would have to be something to do with textscan or writing a loop to scan each line I am just unsure where to start with this. I also have another array thats just date and time which corresponds to the position. Thank you for any help I just want to extract each the lat long and ellipsoidal height either in three separate columns or one. Thank you very much for any help or resources shared as well as your time it's very appreciated.
'$GPGGA,070627.40,4957.091054,N,00616.902179,W,2,18,0.7,2.274,M,54.2,M,4.0,0525*58'
'$GPGGA,070627.60,4957.091029,N,00616.902153,W,2,18,0.7,2.280,M,54.2,M,4.0,0525*53'
'$GPVTG,147.2,T,,M,0.54,N,1.00,K,P*1C'
'$GPGGA,070627.80,4957.091007,N,00616.902128,W,2,18,0.7,2.284,M,54.2,M,4.0,0525*59'
'$GPGGA,070628.00,4957.090985,N,00616.902106,W,2,18,0.7,2.280,M,54.2,M,4.0,0525*54'
'$GPVTG,147.4,T,,M,0.47,N,0.86,K,P*17'
'$GPZDA,070628.00,07,09,2020,00,00*63'
'$GPGGA,070628.20,4957.090955,N,00616.902089,W,2,18,0.7,2.277,M,54.2,M,4.0,0525*55'
'$GPGGA,070628.40,4957.090927,N,00616.902070,W,2,18,0.7,2.273,M,54.2,M,4.0,0525*54'
'$GPGGA,070628.60,4957.090902,N,00616.902045,W,2,18,0.7,2.277,M,54.2,M,4.0,0525*53'
'$GPVTG,146.6,T,,M,0.54,N,1.01,K,P*18'
'$GPGGA,070628.80,4957.090881,N,00616.902018,W,2,18,0.7,2.284,M,54.2,M,4.0,0525*53'
'$GPGGA,070629.00,4957.090860,N,00616.901996,W,2,18,0.7,2.288,M,54.2,M,4.0,0525*55'
'$GPVTG,147.2,T,,M,0.46,N,0.84,K,P*12'
'$GPZDA,070629.00,07,09,2020,00,00*62'
'$GPGGA,070629.20,4957.090836,N,00616.901976,W,2,18,0.7,2.289,M,54.2,M,5.0,0525*5A'
'$GPGGA,070629.40,4957.090809,N,00616.901949,W,2,18,0.7,2.298,M,54.2,M,5.0,0525*5C'
'$GPGGA,070629.60,4957.090779,N,00616.901916,W,2,18,0.7,2.313,M,54.2,M,5.0,0525*5E'
'$GPVTG,144.8,T,,M,0.66,N,1.23,K,P*15'
'$GPGGA,070629.80,4957.090754,N,00616.901888,W,2,18,0.7,2.315,M,54.2,M,5.0,0525*5F'
  3 Comments
Daryna Butash
Daryna Butash on 29 Jul 2021
Hi, I've made something work but it's not quite right I don't think as I am getting an error of: Unable to perform assignment because the size of the left side is 1-by-23 and the size of the right side is 1-by-14.
I am not sure whats going wrong, here is my script so far:
for k1 = 1:size(poss,1)
nmea(k1,:) = str2double(regexp(poss{k1}, '\w*.\w*', 'match'))
contains(poss,'$GPGGA')
end
poss is my dataset which is a cell matrix of 71902x1 cell and each cell has a data string in it. I also tried to create a pattern and a wildcardPattern which somewhat worked but not to my satifaction and I think I am not quite understanding the concept of the regexp function. Any help or suggestions are very welcome and appreciated thank you! I tried to just export the whole string as I couldn't quite figure out how to export specific numbers (character numbers) and I need all the rows that start with $GPGGA the other ones need to be empty or a Nan but I cannot delete theme as I need them in the same place. Thank you again!!

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 29 Jul 2021
Edited: Cris LaPierre on 30 Jul 2021
I would also suggest playing around with the import tool. Here, you can use a UI to find the right settings to import the data, and then generate the corresponding code. This will not do everything you want, but will give you code you can then work with.
Using the data you shared above, here's what I came up with using this approach plus some modification.
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 15);
% Specify range and delimiter
opts.Delimiter = ["'", ","];
% Specify column names and types
opts.VariableTypes = ["string", "double", "double", "categorical", "double", "categorical", "double", "double", "double", "double", "categorical", "double", "categorical", "double", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.LeadingDelimitersRule = "ignore";
opts.TrailingDelimitersRule = "ignore";
opts.SelectedVariableNames = [1 3 5 12];
% Import the data
latlondata = readtable("latlondata.txt", opts);
latlondata(~strcmp(latlondata.Var1,"$GPGGA"),:)={missing}
latlondata = 20×4 table
Var1 Var2 Var3 Var4 _________ ______ _____ ____ "$GPGGA" 4957.1 616.9 54.2 "$GPGGA" 4957.1 616.9 54.2 <missing> NaN NaN NaN "$GPGGA" 4957.1 616.9 54.2 "$GPGGA" 4957.1 616.9 54.2 <missing> NaN NaN NaN <missing> NaN NaN NaN "$GPGGA" 4957.1 616.9 54.2 "$GPGGA" 4957.1 616.9 54.2 "$GPGGA" 4957.1 616.9 54.2 <missing> NaN NaN NaN "$GPGGA" 4957.1 616.9 54.2 "$GPGGA" 4957.1 616.9 54.2 <missing> NaN NaN NaN <missing> NaN NaN NaN "$GPGGA" 4957.1 616.9 54.2
% if you want to get rid of the first column, just remove it
latlondata.Var1 = []
latlondata = 20×3 table
Var2 Var3 Var4 ______ _____ ____ 4957.1 616.9 54.2 4957.1 616.9 54.2 NaN NaN NaN 4957.1 616.9 54.2 4957.1 616.9 54.2 NaN NaN NaN NaN NaN NaN 4957.1 616.9 54.2 4957.1 616.9 54.2 4957.1 616.9 54.2 NaN NaN NaN 4957.1 616.9 54.2 4957.1 616.9 54.2 NaN NaN NaN NaN NaN NaN 4957.1 616.9 54.2

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!