Splitting a sentence into an array of letters
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm new to MATLAB. I would like to learn how to convert or split a string (maybe a sentence or a passange) to an array of letters. For example:
I have a string of 'I am happy'
I want my end result of my array to be [ 'I' ' ' 'a' 'm' ' ' 'h' 'a' 'p' 'p' 'y' '.']
How can I do that? Thank you for all your advice and help.
2 commentaires
Stephen23
le 23 Sep 2022
Because square brackets are a concatenation operator, your two examples are equivalent (apart from the dot at the end, which appears out of nowhere):
A = 'I am happy' % I have a string of
B = [ 'I' ' ' 'a' 'm' ' ' 'h' 'a' 'p' 'p' 'y' '.'] % I want my end result of my array to be
Réponse acceptée
Chunru
le 23 Sep 2022
str = 'I am happy' % characar array
% the char array str is stored as [ 'I' ' ' 'a' 'm' ' ' 'h' 'a' 'p' 'p' 'y'
% '.'] internaly. It only display as 'I am happy' for compactness.
%
% You can get the individual character
str(3)
% if you transpose str, it is a column vector of characters
str'
3 commentaires
Plus de réponses (0)
Voir également
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!