How do I determine which pattern is in a string?

7 ビュー (過去 30 日間)
Nathan O'Donoghue
Nathan O'Donoghue 2021 年 3 月 31 日
コメント済み: Star Strider 2021 年 4 月 7 日
Hi,
I have a table imported from Excel which contains the units of measurement within the Variable Names of the table properties, i.e.
MyTable.Properties.VariableNames = 1x 5 cell array
{'Node Number'} {'X Location (m)'} {'Y Location (m)'} {'Z Location (m)'} {'Normal Stress (Pa)'}
I'm trying to determine which units have been used in the Variable Names from a given list, pat = ["(m)", "(cm)", "(mm)", "(in)", "(ft)"] ;
So far I've tried functions such as contains and strcmp but that's only told me which cells of my variable names contain one of the given patterns, rather than which pattern. Is there a way to do this without having to use contains a separate time for each variable.
Thanks,
Nathan.

採用された回答

Star Strider
Star Strider 2021 年 3 月 31 日
編集済み: Star Strider 2021 年 3 月 31 日
I am not certain what result you want.
Try this:
MyTable.Properties.VariableNames = {{'Node Number'} {'X Location (m)'} {'Y Location (m)'} {'Z Location (m)'} {'Normal Stress (Pa)'}};
Outc = regexp([MyTable.Properties.VariableNames{:}], '\(\w*\)','match')
Out = [Outc{:}]
producing:
Out =
1×4 cell array
{'(m)'} {'(m)'} {'(m)'} {'(Pa)'}
EDIT — (31 Mar 2021 at 15:52)
Another option:
Out = extractBetween([MyTable.Properties.VariableNames{2:end}], '(',')')
producing:
Out =
1×4 cell array
{'m'} {'m'} {'m'} {'Pa'}
The extractBetween function was introduced in R2016b.
  4 件のコメント
Nathan O'Donoghue
Nathan O'Donoghue 2021 年 4 月 7 日
Oh wow!
Star Strider
Star Strider 2021 年 4 月 7 日
Definitely!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by