Create multiple sub arrays in one line!

13 vues (au cours des 30 derniers jours)
Francisco Dias
Francisco Dias le 27 Nov 2019
Commenté : Star Strider le 27 Nov 2019
Hi everyone, I have the string bellow as input to a function
'(44) (hello) (good) ()'
the function looks for indices positions of left and right parenthesis...
open = strfind(text, '(');
close = strfind(text, ')');
after that I want to isolate everything inside the parenthesis...
I was hoping there was a fast way of doing this something like:
output = text(open:close);
but it doesn't seem to work does anyone has a clue if this is possible?!

Réponse acceptée

Star Strider
Star Strider le 27 Nov 2019
Try this:
str = '(44) (hello) (good) ()';
out = regexp(str, '(?<=\()\w*(?=\))', 'match')
producing:
out =
1×3 cell array
{'44'} {'hello'} {'good'}
See the documentation on regexp (and its friends) for details.
Make appripriate changes to get the result you wannt.
  4 commentaires
Francisco Dias
Francisco Dias le 27 Nov 2019
Woow this is great! thank you ;)
Star Strider
Star Strider le 27 Nov 2019
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Turlough Hughes
Turlough Hughes le 27 Nov 2019
Modifié(e) : Turlough Hughes le 27 Nov 2019
This is one way to go about it:
a=strsplit(text(text~=' '),{'(',')'})
output=sprintf('%s',a{:})
  1 commentaire
Turlough Hughes
Turlough Hughes le 27 Nov 2019
Modifié(e) : Turlough Hughes le 27 Nov 2019
Actually, just realising you could also write:
output=text(text~=' ' | text~='(' | text~=')')

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Tags

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by