String starting with letter 's' from cell array

6 vues (au cours des 30 derniers jours)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan le 17 Août 2015
Réponse apportée : Guillaume le 17 Août 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

Réponse acceptée

Guillaume
Guillaume le 17 Août 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'.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by