フィルターのクリア

Extract only numbers from a text file with Matlab

2 ビュー (過去 30 日間)
K. Taieb
K. Taieb 2020 年 6 月 29 日
コメント済み: K. Taieb 2020 年 7 月 1 日
In my txt file I have many lines with the following form:
I want to extract only the numbers that are between “ ” and after and only after x= and y=.
<Point x="-1.16804" y="-0.18000" z="1.60000" i="0.000000" j="0.000000" k="1.000000" u="1.000000" v="0.000000" w="0.000000" />
<Point x="-6.54428" y="-0.12000" z="1.60000" i="0.000000" j="0.000000" k="1.000000" u="1.000000" v="0.000000" w="0.000000" />
<Point x="-0.68936" y="-0.12000" z="1.60000" i="0.000000" j="0.000000" k="1.000000" u="1.000000" v="0.000000" w="0.000000" />
<Point x="-6.85390" y="-0.06000" z="1.60000" i="0.000000" j="0.000000" k="1.000000" u="1.000000" v="0.000000" w="0.000000" />
Can anyone help me?
Thank you.

採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 29 日
I recommend using regexp with named tokens and the 'names' option. For example
parts = regexp(S, 'x="(?<x>[^"])"\s+y="(?<y>[^"])', 'names')
parts would then be a struct array of character vectors with fields x and y
x = str2double({parts.x} );
y = str2double({parts.y} );
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 30 日
str = fileread('code_trajectoire.txt');
parts = regexp(str, 'x="(?<x>[^"]+)"\s+y="(?<y>[^"]+)', 'names');
x=str2double({parts.x});
y=str2double({parts.y});
K. Taieb
K. Taieb 2020 年 7 月 1 日
Great!
Thank you

サインインしてコメントする。

その他の回答 (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