Reading numeric part of line n MATLAB

1 回表示 (過去 30 日間)
Poulomi Ganguli
Poulomi Ganguli 2017 年 9 月 7 日
編集済み: Cam Salzberger 2017 年 9 月 10 日
Hi, I have stream gauge information as attached. I want to read the numeric part of line # 15, i.e., catchment area leaving the string part. I tried it like this:
fid_AR = fopen(['Z:\USER\Eva_Steirou\data_GRDC\',Station_name]);
charCell_AR = textscan(fid_AR,'%s','Delimiter','\n');
fclose(fid_AR);
ARLine=charCell_AR{1}{15}
I could extract line 15, but don't know how to extract only numeric part of the line in number form.

採用された回答

Cam Salzberger
Cam Salzberger 2017 年 9 月 7 日
編集済み: Cam Salzberger 2017 年 9 月 10 日
Hello Poulomi,
If you know that you'll only ever have a single colon (:) character in the row, you can use that to split off the numeric part. Here's an example of how to do it with regular expressions:
s = '# Catchment area (km²): 122.300';
tok = regexp(s,'[^:]+:\s+([\S]+)','tokens');
str2double(tok{1}{1})
But you could do it even simpler with strsplit and strtrim:
c = strsplit(s,':');
str2double(strtrim(c{2}))
-Cam

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by