How to read .txt separate by - and in dms format
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I like to convert a .txt file in longitude and latitude data (DMS) to UTM (especially MTM 7 nad 83)
I have found the following command to do this : https://www.mathworks.com/help/map/ref/geodetic2ecef.html or https://www.mathworks.com/matlabcentral/fileexchange/9696-geodetic-transformations
But I don't succed in reading the .txt file and the format is a bit tricky.
I have this type of file :
"Lat (DMS)" "Long (DMS)" "Depth (m)"
46-34-50.894N 071-59-59.280W 0.30000
46-34-53.066N 071-59-56.016W 0.60000
46-34-55.928N 071-59-59.853W 1.80000
46-34-54.546N 071-59-58.198W 1.20000
...
And I would like to have a matrix with 7 columns like that :
46 34 50.894 071 59 59.280 0.30000
46 34 53.066 071 59 56.016 0.60000
46 34 55.928 071 59 59.853 1.80000
46 34 54.546 071 59 58.198 1.20000
...
I dont know how to import the file and separate the number because the deliminater is '-' and '[space]' and I also want to get rip of N and W...
Do you know how to do this ?
Thanks
0 Commenti
Risposta accettata
dpb
il 2 Apr 2021
>> fmt='%f-%f-%fN %f-%f-%fW %f';
>> data=cell2mat(textscan(fid,fmt,'CollectOutput',1,'HeaderLines',1))
data =
46.00 34.00 50.89 71.00 59.00 59.28 0.30
46.00 34.00 53.07 71.00 59.00 56.02 0.60
46.00 34.00 55.93 71.00 59.00 59.85 1.80
46.00 34.00 54.55 71.00 59.00 58.20 1.20
>> fid=fclose(fid);
NB: Above works only if the coordinates are all N and W; otherwise one will have to either skip a character in the format scan string and trust can figure out what is what from numeric values or return it as string variable.
But, there's nothing really magic, just write an appropriate format string for the file format.
Or, you could read the whole thing in as a cellstr() array and then split() on the delimiters if wanted to do that way.
2 Commenti
dpb
il 2 Apr 2021
That's just the file handle for textscan...
fid=fopen('yourfilename.txt','r');
Più risposte (1)
Cris LaPierre
il 2 Apr 2021
Modificato: Cris LaPierre
il 2 Apr 2021
I was trying to avoid using a formatspec, but I can't think of a good way. Here's how I would probably do it. The result is a table. See this page on how to access data in a table. How you access it determines if the extracted values are in a table or an array.
data = readtable("NONNA100_4600N07100W.txt",'HeaderLines',1,"ReadVariableNames",false,...
'Format','%f-%f-%f%*c %f-%f-%f%*c %f')
You could probably use this same formatspec with textscan, which will eliminate needing to worry about the character indicating cardinal direction.
3 Commenti
Cris LaPierre
il 3 Apr 2021
Modificato: Cris LaPierre
il 3 Apr 2021
Not a critique. All credit to you. Just pointing out the format for someone who doesn't understand it.
Vedere anche
Categorie
Scopri di più su String Parsing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!