Splitting a sentence into an array of letters
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ben Nguyen
am 22 Sep. 2022
Kommentiert: Ben Nguyen
am 23 Sep. 2022
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 Kommentare
Stephen23
am 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
Akzeptierte Antwort
Chunru
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!