Extract number after specific words

13 次查看(过去 30 天)
I have an excel file with text and number. In the same cell I have text with value of latitude and longitude value. Exist one method to get numbers, specificing the text.
For example: you extract the number after the word "latitude", you extract number after word "longitude".
  2 个评论
Kevin Holly
Kevin Holly 2021-8-19
Rachele,
Is the text consistent?
If the string is “latitude30”or “latitude3” for instance, you could use sscanf.
str = "latitude30";
sscanf(str,'latitude%f')
Rachele Franceschini
for example the column name is: entities.
Within there is text with also latitude 45,0000 and longitude 2,00000.
I would like the number after words.

请先登录,再进行评论。

采纳的回答

DGM
DGM 2021-8-19
编辑:DGM 2021-8-19
Something like this?
C = {'latitude 45,0000','longitude 2,0000';
'latitude 47,5000','longitude 5,0000';
'latitude 50,8000','longitude 10,0000'};
D = regexp(C,'(?<=[latitude |longitude ])\d+,?\d*','match');
D = str2double(reshape(strrep(vertcat(D{:}),',','.'),size(C)))
D = 3×2
45.0000 2.0000 47.5000 5.0000 50.8000 10.0000
That's assuming that all the lines are formatted the same and that the comma is the decimal separator.
  2 个评论
Rachele Franceschini
编辑:Rachele Franceschini 2021-8-20
Thank you, but I don't have the same format in all the lines and the sometimes I have comma and dot.
How can I do?
for example in this case
C = {aa bbb cccccc dddddd, latitude 45,0000 longitude 2,0000;
aaa bbbb cc, latitude 46,00000 longitude 2,00000;
aaaaa bbbbb cccc ddd eeee fffffff, latitude 46,00000 longitude 2,00000 latitude 49,00000 longitude 9,00000}
DGM
DGM 2021-8-20
编辑:DGM 2021-8-27
Without formatting, this is ambiguous.
C = {aa bbb cccccc dddddd, latitude 45,0000 longitude 2,0000;
aaa bbbb cc, latitude 46,00000 longitude 2,00000;
aaaaa bbbbb cccc ddd eeee fffffff, latitude 46,00000 longitude 2,00000 latitude 49,00000 longitude 9,00000}
I'm going to assume it's just a 3x1 array with a double entry on the third line
% extra prefix chars per line
% mixed comma/dot decimal sep
% multiple lat/lon per row
C = { 'aa bbb cccccc dddddd, latitude 45.0500 longitude 2.0500';
'aaa bbbb cc, latitude 46,00000 longitude 2,00000';
'aaaaa bbbbb cccc ddd eeee fffffff, latitude 46,00000 longitude 2,00000 latitude 49,00000 longitude 9,00000'};
D = regexp(C,'(?<=[latitude |longitude ])\d+[,|.]?\d*','match');
D = cellfun(@(x) reshape(x.',2,[]).',D,'uniform',false);
D = str2double(reshape(strrep(vertcat(D{:}),',','.'),[],2))
D = 4×2
45.0500 2.0500 46.0000 2.0000 46.0000 2.0000 49.0000 9.0000
Note that there are more rows in D than in C, since some lines have multiple entries.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Geographic Plots 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by