How to split letters in a word into an array

78 次查看(过去 30 天)
Ex: In the word 'HELLO', extract the letters 'H' 'E' 'L' 'L' 'O'

采纳的回答

Jan
Jan 2013-7-1
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 个评论
Stephen23
Stephen23 2018-2-7
编辑:Stephen23 2021-5-26
Try num2cell, e.g. where W is your word (a 1xN character vector):
C = num2cell(W(:))
Adam Danz
Adam Danz 2021-5-25
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.

请先登录,再进行评论。

更多回答(2 个)

Tom
Tom 2013-7-1
编辑:Tom 2013-7-1
str = 'HELLO';
cellstr(str')'

Octa
Octa 2013-7-2
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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by