How to remove everything from string except what's inside square brackets?

I have this cell array:
{'0,11:1.03 SPEED MEASURED 1 [rpm]'}
{'0,12:1.03 SPEED MEASURED 1 [rpm]'}
{'0,13:1.03 SPEED MEASURED 1 [rpm]'}
{'0,14:1.03 SPEED MEASURED 1 [rpm]'}
{'0,11:1.06 MOTOR CURRENT [A]' }
{'0,12:1.06 MOTOR CURRENT [A]' }
{'0,13:1.06 MOTOR CURRENT [A]' }
{'0,14:1.06 MOTOR CURRENT [A]' }
{'0,11:2.13 TORQ USED REF [%]' }
{'0,12:2.13 TORQ USED REF [%]' }
{'0,13:2.13 TORQ USED REF [%]' }
{'0,14:2.13 TORQ USED REF [%]' }
{'0,11:1.08 MOTOR TORQUE [%]' }
{'0,12:1.08 MOTOR TORQUE [%]' }
{'0,13:1.08 MOTOR TORQUE [%]' }
{'0,14:1.08 MOTOR TORQUE [%]' }
{'0,11:2.10 TORQUE REF 3 [%]' }
{'0,12:2.10 TORQUE REF 3 [%]' }
{'0,13:2.10 TORQUE REF 3 [%]' }
{'0,14:2.10 TORQUE REF 3 [%]' }
How can I get the unit from between the brackets in each string, so that I'm left with the following:
{'rpm'}
{'rpm'}
{'rpm'}
{'rpm'}
{'A' }
{'A' }
...
{'%' }
Thanks!

6 件のコメント

KSSV
KSSV 2019 年 7 月 12 日
regexp this is the saviour.
Thanks! This is the command I ended up using:
regexp(headers{2}, '(?<=\[).*?(?=\])', 'match');
KSSV
KSSV 2019 年 7 月 12 日
Thats great...this function makes me always confused......
Heidi Mäkitalo
Heidi Mäkitalo 2019 年 7 月 12 日
Yeah me too. I always struggle with it, even when I'm trying to achieve something really simple (like in this case) ?
Akira Agata
Akira Agata 2019 年 7 月 12 日
How about extractBetween function?
Heidi Mäkitalo
Heidi Mäkitalo 2019 年 7 月 12 日
Wow, I never even knew about this function! Very intuitive and seems to work just as well as regexp. Are there any benefits to using extractBetween other than the fact that it's more easy to use for this purpose?

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

回答 (1 件)

Stephen23
Stephen23 2019 年 7 月 12 日
編集済み: Stephen23 2019 年 7 月 12 日
Where C is your cell array:
>> D = regexp(C,'\[(.+)\]','tokens','once')
>> D = vertcat(D{:})
D =
'rpm'
'rpm'
'rpm'
'rpm'
'A'
'A'
'A'
'A'
'%'
'%'
'%'
'%'
'%'
'%'
'%'
'%'
'%'
'%'
'%'
'%'

カテゴリ

ヘルプ センター および File ExchangeSpecialized Power Systems についてさらに検索

タグ

タグが未入力です。

質問済み:

2019 年 7 月 12 日

編集済み:

2019 年 7 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by