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

3 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2021-4-2
>> 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 个评论
Thomas prn
Thomas prn 2021-4-2
编辑:Thomas prn 2021-4-2
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 2021-4-2
That's just the file handle for textscan...
fid=fopen('yourfilename.txt','r');

请先登录,再进行评论。

更多回答(1 个)

Cris LaPierre
Cris LaPierre 2021-4-2
编辑:Cris LaPierre 2021-4-2
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 个评论
Cris LaPierre
Cris LaPierre 2021-4-3
编辑:Cris LaPierre 2021-4-3
Not a critique. All credit to you. Just pointing out the format for someone who doesn't understand it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by