フィルターのクリア

Splitting a sentence into an array of letters

1 回表示 (過去 30 日間)
Ben Nguyen
Ben Nguyen 2022 年 9 月 22 日
コメント済み: Ben Nguyen 2022 年 9 月 23 日
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 件のコメント
Stephen23
Stephen23 2022 年 9 月 23 日
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
A = 'I am happy'
B = [ 'I' ' ' 'a' 'm' ' ' 'h' 'a' 'p' 'p' 'y' '.'] % I want my end result of my array to be
B = 'I am happy.'
Ben Nguyen
Ben Nguyen 2022 年 9 月 23 日
My apologies, there would be no dot at the end of the array.
My goal is to break a string into each element or letter and strore them in a array.

サインインしてコメントする。

採用された回答

Chunru
Chunru 2022 年 9 月 23 日
str = 'I am happy' % characar array
str = 'I am happy'
% 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)
ans = 'a'
% if you transpose str, it is a column vector of characters
str'
ans = 10×1 char array
'I' ' ' 'a' 'm' ' ' 'h' 'a' 'p' 'p' 'y'
  3 件のコメント
Chunru
Chunru 2022 年 9 月 23 日
str = "I am happy !"
str = "I am happy !"
ch = char(str)'
ch = 12×1 char array
'I' ' ' 'a' 'm' ' ' 'h' 'a' 'p' 'p' 'y' ' ' '!'
Ben Nguyen
Ben Nguyen 2022 年 9 月 23 日
This should work. Thank you !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by