How to read .txt separate by - and in dms format

3 visualizaciones (últimos 30 días)
Thomas prn
Thomas prn el 2 de Abr. de 2021
Comentada: dpb el 3 de Abr. de 2021
Hello,
I like to convert a .txt file in longitude and latitude data (DMS) to UTM (especially MTM 7 nad 83)
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

Respuesta aceptada

dpb
dpb el 2 de Abr. de 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 comentarios
Thomas prn
Thomas prn el 2 de Abr. de 2021
Editada: Thomas prn el 2 de Abr. de 2021
Ok thank you. Yeah it's not a big deal but I don't realy understand the symbole in the fmt command so I was blocked.
What di you put in your fid file please ?
dpb
dpb el 2 de Abr. de 2021
That's just the file handle for textscan...
fid=fopen('yourfilename.txt','r');

Iniciar sesión para comentar.

Más respuestas (1)

Cris LaPierre
Cris LaPierre el 2 de Abr. de 2021
Editada: Cris LaPierre el 2 de Abr. de 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')
data = 20896×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 ____ ____ ______ ____ ____ ______ ____ 46 50 3.517 71 0 0.566 -1.4 46 50 5.945 71 0 0.239 -0.2 46 50 4.438 70 59 55.439 -2.3 46 50 6.363 70 59 50.55 -0.5 46 50 6.851 70 59 45.749 -0.7 46 50 9.081 70 59 59.927 -0.4 46 50 9.577 70 59 55.418 -0.2 46 50 9.191 70 59 50.348 -0.7 46 50 9.429 70 59 45.541 -0.8 46 50 8.59 70 59 41.131 -0.3 46 50 10.014 70 59 36.262 -1.1 46 50 13.191 71 0 0.016 -0.3 46 50 12.418 70 59 55.431 -0.8 46 50 12.593 70 59 50.06 -1.6 46 50 12.593 70 59 45.648 -1.2 46 50 11.827 70 59 41.264 -0.8
You could probably use this same formatspec with textscan, which will eliminate needing to worry about the character indicating cardinal direction.
  3 comentarios
Cris LaPierre
Cris LaPierre el 3 de Abr. de 2021
Editada: Cris LaPierre el 3 de Abr. de 2021
Not a critique. All credit to you. Just pointing out the format for someone who doesn't understand it.
dpb
dpb el 3 de Abr. de 2021
Not taken as such..."Just sayin..." :)

Iniciar sesión para comentar.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by