How to split letters in a word into an array

 Akzeptierte Antwort

Jan
Jan am 1 Jul. 2013

0 Stimmen

The string 'Hello' consists of single characters already:
str = 'Hello';
for k = 1:length(str)
disp(str(k))
end
So please explain the wanted type and dimensions of the output. 'H' 'E' 'L' 'L' 'O' is not clear enough.

4 Kommentare

I've had a similar question a few times - my goal is take a string that represents RNA, e.g. seq = 'ATTAGCTG', and then use the individual letters as the xticklabels in a figure. cellstr(seq) would return a cell containing the entire string. I believe what I'm looking for is a cell array of individual letters (no loops would be nice). Forgive me if I'm missing something simple.
Nevermind! I just noticed the transpose in the below comment. Learn something new every day.
Stephen23
Stephen23 am 7 Feb. 2018
Bearbeitet: Stephen23 am 26 Mai 2021
Try num2cell, e.g. where W is your word (a 1xN character vector):
C = num2cell(W(:))
Adam Danz
Adam Danz am 25 Mai 2021
num2cell is the best solution. In case str is of class string
c = num2cell(char(str));
This works when str is a character array or a string.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Tom
Tom am 1 Jul. 2013
Bearbeitet: Tom am 1 Jul. 2013

8 Stimmen

str = 'HELLO';
cellstr(str')'
Octa
Octa am 2 Jul. 2013

0 Stimmen

If you want to extract the letters, simply extract in this way
>> str(1)
H
>> str(2)
E
>> str(3)
L
>>str(4)
L
>> str(5)
L
>> str(6)
O

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Gefragt:

MKN
am 1 Jul. 2013

Bearbeitet:

am 26 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by