Extract Variables from mixed string

1 回表示 (過去 30 日間)
Azar Nejad
Azar Nejad 2018 年 8 月 12 日
コメント済み: Azar Nejad 2018 年 8 月 12 日
Hi all,
In this string
'# Message: onset_pic1_8.png'
how can I read 'onset', 'pic', '1', '8' in four variables?
  1 件のコメント
dpb
dpb 2018 年 8 月 12 日
Use regexp

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 8 月 12 日
S = '# Message: onset_pic1_8.png';
parts = regexp(S, '(?<name1>[A-Za-z]+)_(?<name2>[A-Za-z]+)(?<num1>\d+)_(?<num2>\d+)', 'names')
parts =
struct with fields:
name1: 'onset'
name2: 'pic'
num1: '1'
num2: '8'
I coded to permit uppercase as well as lowercase, but I did assume that the alphabetic parts remain alphabetic and the numeric parts remain numeric.
  1 件のコメント
Azar Nejad
Azar Nejad 2018 年 8 月 12 日
This works great many thanks
Azar

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

その他の回答 (1 件)

Paolo
Paolo 2018 年 8 月 12 日
編集済み: Paolo 2018 年 8 月 12 日
You can use:
mystring = '# Message: onset_pic1_8.png';
matches = regexp(mystring,'\d|[a-z]+(?=_|\d)','match')
>>matches
{'onset'} {'pic'} {'1'} {'8'}
To obtain the values.
You can't use 1 and 8 as variable names.

カテゴリ

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