How can i read multiple values in a string?

3 ビュー (過去 30 日間)
Marco Silva
Marco Silva 2019 年 8 月 12 日
コメント済み: Marco Silva 2019 年 8 月 12 日
Hi guys,
I have a problem getting some values.
Basically, i have a variable (string) that always has the same format but with a variable length.
Some examples:
  1. " H:2T:16799C:7| H:2T:16803C:15 | H:1T:17399C:11 "
  2. " H:4T:16500C:7 "
  3. " H:6T:568C:20| H:5T:313929C:11 "
How we can see, the variable has the same format in all examples but can have a variable number of repetitions.
I want to read the values, for the first example: 2 - 16799 - 7 | 2 - 16803 - 15 | 1 - 17399 - 11.
How can i do this dynamically? To be able to read in all cases.
Thanks.

採用された回答

Joel Handy
Joel Handy 2019 年 8 月 12 日
編集済み: Joel Handy 2019 年 8 月 12 日
I believe a regular expression along these lines is what you are looking for.
data = regexp('1º:" H:2T:16799C:7| H:2T:16803C:15 | H:1T:17399C:11 "', ...
'H:(?<H>\d+)T:(?<T>\d+)C:(?<C>\d+)', 'names')
H = str2double({data.H})
T = str2double({data.T})
C = str2double({data.C})
If these are all lines in a file, you can read the entire file into a signle string and process everything at once.
MyString = sprintf('1º:" H:2T:16799C:7| H:2T:16803C:15 | H:1T:17399C:11 "\n2º:" H:4T:16500C:7 "\n3º:" H:6T:568C:20| H:5T:313929C:11 "')
regexp(MyString, 'H:(?<H>\d+)T:(?<T>\d+)C:(?<C>\d+)', 'names')
  1 件のコメント
Marco Silva
Marco Silva 2019 年 8 月 12 日
The regular expression works perfectly. Thank you !!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by