String starting with letter 's' from cell array

6 Ansichten (letzte 30 Tage)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan am 17 Aug. 2015
Beantwortet: Guillaume am 17 Aug. 2015
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

Akzeptierte Antwort

Guillaume
Guillaume am 17 Aug. 2015
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'.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by