String starting with letter 's' from cell array

7 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Guillaume
Guillaume il 17 Ago 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'.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by