How to join the subelements of a cell?

I am trying to express all sentences in one cell with string subelements.
s='I am George. John is there? Ana is around! Are you mad?'
[y,matches] = strsplit(s,{'.','?','!'})
for i = 1:length(matches)
y{i}=[y{i},matches{i}]
end
y=y(~cellfun('isempty',y))

1 commentaire

Matt J
Matt J le 26 Jan 2018
Don't think you finished your question (because I'm not sure what it is). If you're trying to rejoin all the sub-strings in y, see my answer below.

Connectez-vous pour commenter.

Réponses (2)

Matt J
Matt J le 26 Jan 2018
Modifié(e) : Matt J le 26 Jan 2018
s=[y{:}]

6 commentaires

GEORGIOS BEKAS
GEORGIOS BEKAS le 26 Jan 2018
Modifié(e) : GEORGIOS BEKAS le 26 Jan 2018
isa(s,'cell') =0
s={[y{:}]}
GEORGIOS BEKAS
GEORGIOS BEKAS le 26 Jan 2018
the problem is that the strings are not separate. C[1,1] = 'I am George.' 'John is there?' 'Ana is around!' 'Are you mad?'
Guillaume
Guillaume le 26 Jan 2018
What does are not separate mean?
Please use valid matlab syntax. Otherwise it doesn't help in explaining at all.
GEORGIOS BEKAS
GEORGIOS BEKAS le 26 Jan 2018
seperate strings into one cell subelement.
This is what I receive when I run the code you posted followed by Matt's suggestion.
s = 'I am George. John is there? Ana is around! Are you mad?'
[y,matches] = strsplit(s,{'.','?','!'})
for i = 1:length(matches)
y{i}=[y{i},matches{i}]
end
y=y(~cellfun('isempty',y))
s2 = [y{:}]
whos s2
s2 =
'I am George. John is there? Ana is around! Are you mad?'
Name Size Bytes Class Attributes
s2 1x55 110 char
What exactly do you need/want/expect s2 to be for this example? Be specific about the type and size you expect s2 to be and the contents you expect s2 to contain.

Connectez-vous pour commenter.

Domanic
Domanic le 26 Jan 2018
Modifié(e) : Domanic le 26 Jan 2018
To join elements of the cell array, y, you can use:
y = cell2mat(y);
or
y = {cell2mat(y)};
for a cell array.
To more generally achieve what you're trying to do, you can use regular expressions:
expression = '(?<=\W)\s'; % Expression to find white space following punctuation
strsplit(s,expression,'delimitertype','regularexpression'); % split string around result of regexp

2 commentaires

GEORGIOS BEKAS
GEORGIOS BEKAS le 26 Jan 2018
the problem is that the strings are not separate. C[1,1] = 'I am George.' 'John is there?' 'Ana is around!' 'Are you mad?'
How about:
expression = '(?<=\W)\s';
y = strsplit(s,expression,'delimitertype','regularexpression');
y = {cell2struct(y,'substrings')};
where the strings are accessed as:
y{1}.substrings
or
y{1}.substrings(1)
not sure why you'd want this, though.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Produits

Commenté :

le 26 Jan 2018

Community Treasure Hunt

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

Start Hunting!

Translated by