Converting entire cell array to strings

x={'words',10,20,30,'more words'}
How do I convert the entire cell array above to strings?

2 件のコメント

Image Analyst
Image Analyst 2015 年 6 月 3 日
x{1} and x{5} are already strings. Do you want to scan the cell array and convert the cells that have numbers in them to strings? Or do you want 5 separate string variables, like x1, x2, x3, x4, and x5?
x1 = x{1}
x2 = num2str(x{2});
x3 = num2str(x{3});
x4 = num2str(x{4});
x5 = x{5};
Rainer
Rainer 2015 年 6 月 3 日
編集済み: Rainer 2015 年 6 月 3 日
I want to have a generalized solution where I can convert a cell array which is randomly populated with doubles and strings.
{'words',10,20,30,'more words',10,9} becomes {'words','10','20','30','more words','10','9'}
{94,'cows',10,'dogs',30,10,'cats','birds'} becomes {'94','cows','10','dogs','30','10','cats','birds'}

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

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 3 日

6 投票

x={'words',10,20,30,'more words'}
out=cellfun(@num2str,x,'un',0)

2 件のコメント

Guillaume
Guillaume 2015 年 6 月 3 日
編集済み: Guillaume 2015 年 6 月 5 日
It works because num2str just return the string unchanged when given a string.
edit: As pointed out by per, this relies on the undocumented and actually unexpected behaviour of num2str. Such code could break in the future. (But mathworks also often breaks documented behaviour)
Rainer
Rainer 2015 年 6 月 3 日
Excellent! Thanks Azzi

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

その他の回答 (1 件)

per isakson
per isakson 2015 年 6 月 3 日

1 投票

is this close to what you want?
>> strsplit( strjoin(x), ',')
ans =
'words' '10' '20' '30' 'more words'

7 件のコメント

Rainer
Rainer 2015 年 6 月 3 日
strjoin doesn't work because 10,20 and 30 aren't strings?
>> strjoin(x)
Error using strjoin (line 52) First input must be a 1xN cell array of strings.
per isakson
per isakson 2015 年 6 月 3 日
編集済み: per isakson 2015 年 6 月 3 日
On R2013a it works
>> version
ans =
8.1.0.604 (R2013a)
>> x={17,'words',10,20,30,'more words'}
x =
[17] 'words' [10] [20] [30] 'more words'
>> strsplit( strjoin(x), ',' )
ans =
'17' 'words' '10' '20' '30' 'more words'
but not on R2014a
>> version
ans =
8.3.0.532 (R2014a)
>> x={17,'words',10,20,30,'more words'}
strsplit( strjoin(x), ',' )
x =
[17] 'words' [10] [20] [30] 'more words'
Error using strjoin (line 52)
First input must be a 1xN cell array of strings.
Maybe that is too "smart" and was removed
Guillaume
Guillaume 2015 年 6 月 4 日
I'd say it's a good thing it's not behaving like that anymore as I would not expect a string joining function to also perform numeric to string conversion. Feature creep and all that...
per isakson
per isakson 2015 年 6 月 4 日
Indeed that is a good thing. However, I find it worrying that this "feature" was deliberately included in the first place. Especially since strjoin is a fairly recent function.
per isakson
per isakson 2015 年 6 月 4 日
編集済み: per isakson 2015 年 6 月 4 日
The solution with num2str relies on the undocumented (AFAIK) feature that a string input is returned as output.
And I don't find a suitable words to comment on
>> num2str('ten')
ans =
ten
>> int2str('ten')
ans =
116 101 110
Guillaume
Guillaume 2015 年 6 月 5 日
You're right the behaviour of num2str with string inputs is not documented and kind of unexpected. If num2str adhered to its documentation it should throw an error when passed a string. Sloppy coding or sloppy documenting.
Perhaps matlab should have a ToString function like java and .Net.
The behaviour of int2str makes sense to me. It's equivalent to
num2str(uint16('ten'));
per isakson
per isakson 2015 年 6 月 5 日
編集済み: per isakson 2015 年 6 月 5 日
It doesn't help the beginner, me included, that the two functions, num2str and int2str handle string inputs differently.
Yes, "the behavior of int2str makes sense" and that's because it dates back to the beginning of Matlab when everything was numerical arrays. A string was just another way to interpret the array. (My mental model.) By the way, I use strfind to search for sequences of whole numbers in double arrays. I think of that as "us' trick".
However, this behavior of int2str is not documented(?)
There are a number of fundamental function, which converts strings to vectors of ascii-numbers. That is not explicitly stated in the documentation(?)
>> real('ten')
ans =
116 101 110
>> uint16('ten')
ans =
116 101 110
Enough ranting for now!

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2015 年 6 月 3 日

編集済み:

2015 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by