How to convert a 1x1 cell to a string?
4,077 ビュー (過去 30 日間)
古いコメントを表示
bh dhouha
2015 年 2 月 1 日
編集済み: MathWorks Support Team
2024 年 11 月 13 日 3:58
How to convert a 1x1 cell like {'line'} to a character vector like 'line', or a string like "line" please. thx
0 件のコメント
採用された回答
Azzi Abdelmalek
2024 年 11 月 13 日 0:00
編集済み: MathWorks Support Team
2024 年 11 月 13 日 3:58
この 回答 は Walter Roberson
さんによってフラグが設定されました
To convert a cell array of character vectors to a character array, use the “char” function. A = {'line'} B = char(A) To extract the contents from a cell, index using curly braces. A = {'line'} B = A{1} Starting in R2016b, you can store text in string arrays. To convert a cell array to a string array, use the “string” function. A = {'line'} B = string(A) For more information, please refer to the MathWorks documentation on Cell Arrays of Character Vectors and Text in String and Charater Arrays.
6 件のコメント
その他の回答 (2 件)
Image Analyst
2015 年 2 月 1 日
Azzi showed you how to extract the string from a cell. Another way is to convert the cell with char():
ca={'line'} % This is our cell array.
str = char(ca) % Convert it to a character array (string).
Net, both give the same result, just different ways of getting there. If your cell array is only a single solitary cell, then you should not even use a cell in the first place - use a string.
1 件のコメント
Image Analyst
2015 年 2 月 1 日
Morteza Darvish Morshedi
2019 年 6 月 14 日
Even if you have more than 1 string in our cell array, an easy way can be:
S = {'Hello',' ','world'}
ss = [S{:}]
5 件のコメント
Image Analyst
2021 年 2 月 15 日
Just a point of clarification. ss is a character array while str is a string. A few versions ago, MATLAB made these different types of variables, though in common speech, people call either one "strings".
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!