String starting with letter 's' from cell array

2 ビュー (過去 30 日間)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2015 年 8 月 17 日
回答済み: Guillaume 2015 年 8 月 17 日
I have a cell array a = {'sa_dfa','soft_df1','sock_dd2','saz_dfa_d2','suu_f'}
How to extract only the string starting with letter 's' but need to exclude the string starting with soft and sock
so a = {'sa_dfa',[],[],'saz_dfa_d2','suu_f'}
How can I do this?
Thanks a lot

採用された回答

Guillaume
Guillaume 2015 年 8 月 17 日
As per Stalin's answer you can use strncmp and related with logical operators:
a(strncmp(a, 's', 1) & ~strncmp(a, 'sock', numel('sock')) & ~strncmp(a, 'soft', numel(sock)))
Or you can use a regular expression:
a(~cellfun(@isempty, regexp(a, '^s(?!ock|oft)')))
The above regular expression matches any string that starts with 's' not followed by 'ock' or 'oft'.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by