Convert string to cell array

700 ビュー (過去 30 日間)
Artyom
Artyom 2014 年 8 月 31 日
コメント済み: Image Analyst 2021 年 4 月 17 日
How to convert string
a b 3
into cell array {'a' 'b' 3}

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 8 月 31 日
Artyom - try using textscan to read the data from the string. Something like
str = 'a b 3';
Z = textscan(str,'%s','Delimiter',' ')';
Z{:}'
ans =
'a' 'b' '3'
We use the fact that each character in he input string, str, is separated by a space. The space then acts as the delimiter between each character which we can use to separate each into its own string.
Note that Z is a 3x1 cell array.
Try the above and see what happens!
  2 件のコメント
Image Analyst
Image Analyst 2014 年 8 月 31 日
I think he wanted the 3 to be a number, if you look at his desired output. But your method is more flexible than mine. Mine did exactly what he asked but we know that people usually come back and say "but it did not work for the string 'a b 3 xyz 2.718281828'. Yours should handle that, if you can convert numbers from char to double.
Geoff Hayes
Geoff Hayes 2014 年 8 月 31 日
You're right - I hadn't caught that! I just assumed that since the input was a string, then the output would be the same.

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

その他の回答 (3 件)

CHADCHAVAN RATTANASOPA
CHADCHAVAN RATTANASOPA 2018 年 11 月 11 日
cellstr()
  2 件のコメント
Souarv De
Souarv De 2021 年 4 月 17 日
Good.
Image Analyst
Image Analyst 2021 年 4 月 17 日
@Souarv De, why "Good"? This answer is wrong. Just look
str = 'a b 3';
ca = cellstr(str) % CHADCHAVIN's wrong answer
Z = textscan(str, '%s', 'Delimiter',' ')' % Geoff's answer
celldisp(Z)
ca =
1×1 cell array
{'a b 3'}
Z =
1×1 cell array
{3×1 cell}
Z{1}{1} =
a
Z{1}{2} =
b
Z{1}{3} =
3
CHADCHAVIN's answer does not give a cell array with three cells (each one containing a substring). It gives one cell (not 3) and it simply put the string into the one cell. Not what was asked for.
It must have been accepted by mistake so I'm going to unaccept it and accept Geoff's instead.

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


Image Analyst
Image Analyst 2014 年 8 月 31 日
Try this:
str = 'a b 3'
str(str==' ') = []; % Remove spaces.
ca = {str(1),str(2),str(3)} % Create the cell array.
celldisp(ca); % Display its values in the command window.

Martin Vatshelle
Martin Vatshelle 2019 年 3 月 25 日
編集済み: Martin Vatshelle 2019 年 3 月 25 日
If you have a mix of text and numbers textscan as suggested by Geoff is a good solution.
If you only have text strsplit does exactly what you want and I feel that is a bit simpler to use.
strsplit('ab cd ef')
ans =
1×3 cell array
'ab' 'cd' 'ef'
You can specify the delimiter if it is not spaces that are your delimiters.
If you however want to split a string into single characters you could use cellstr
s = 'ab cd ef';
cellstr(s(:))' %here I transposed at the end for readability, you can skip that
ans =
1×8 cell array
'a' 'b' '' 'c' 'd' '' 'e' 'f'

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by