Convert Cell to string

I have got a simple question. I want to convert a cell array to a string array, somehow I don't succeed in doing this.
An example:
I' ve got this: a={1650}, which is a cell array. Because I want to concatenate this with other strings I need to convert the cell array to a string array. I have tried many converting functions but I dont get the correct final string array.
Hopefully somebody can help me with this.
Many Thanks.

回答 (2 件)

Jonathan Thomet
Jonathan Thomet 2020 年 11 月 12 日

5 投票

You have this in the documentation.
C = {'Li','Sanchez','Jones','Yang','Larson'}
B = string(C)
That should do the trick.

9 件のコメント

Walter Roberson
Walter Roberson 2020 年 11 月 12 日
Note that string() objects did not exist until R2016b.
Jim Nicholson
Jim Nicholson 2022 年 5 月 1 日
'B = string(C)' is neat, but neater still if Mathworks created a 'cell2str' function. The question about converting cell to string occurs too often to be ignored.
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 2 日
@Floyd notes on @Jim Nicholson's comment - "This user is correct and the issue should be addressed."
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 2 日
@Floyd @Jim Nicholson - How would cell2str() de different than using string() on a cell array?
Walter Roberson
Walter Roberson 2024 年 1 月 2 日
string() on a cell array returns a string array.
Sometimes what people want is to get a single string (or character vector) that represents the entire cell array.
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 2 日
That can be (easily) achieved -
C = {'Li','Sanchez','Jones','Yang','Larson'}
C = 1×5 cell array
{'Li'} {'Sanchez'} {'Jones'} {'Yang'} {'Larson'}
%If output needed as char
out1 = [C{:}]
out1 = 'LiSanchezJonesYangLarson'
%If output needed as string
out2 = string(out1)
out2 = "LiSanchezJonesYangLarson"
Floyd
Floyd 2024 年 1 月 2 日
編集済み: Floyd 2024 年 1 月 2 日
There are flaws in the string() function, for example I'm currently working on a script where I need to convert the cell array to a table for rptgen, yet during the conversion data values are changed. If you try to convert the data to string first from cell, when converting to a table it produces errors. This is just my most recent issue, though I'm still looking into a way around this.
Walter Roberson
Walter Roberson 2024 年 1 月 2 日
To avoid changing values during conversion, use compose instead of plain string . You will need %.754g for exact conversion of numeric values.
Floyd
Floyd 2024 年 1 月 2 日
@Walter Roberson, I'll give this a go. I appreciate it

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

Youssef  Khmou
Youssef Khmou 2013 年 2 月 23 日

3 投票

hi, You can accomplish that by converting the cell to matrix first then coverting the matrix to string array
B=num2str(cell2mat(A));

3 件のコメント

Patrik Ek
Patrik Ek 2014 年 12 月 15 日
This is one way, but it does not work for the general case. In many cases where cells are used is it because there is different data in different cells. Do you know a general way of doing this?
Walter Roberson
Walter Roberson 2020 年 11 月 12 日
B = cellfun(@val2str, A, 'uniform', 0);
function str = val2str(val)
str = evalc(disp(val));
end
Stephen23
Stephen23 2024 年 1 月 3 日
Or without EVALC, since R2021a:
B = cellfun(@formattedDisplayText, A, 'uni', 0);

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

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2013 年 2 月 23 日

コメント済み:

2024 年 1 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by