필터 지우기
필터 지우기

Converting entire cell array to strings

조회 수: 39 (최근 30일)
Rainer
Rainer 2015년 6월 3일
편집: per isakson 2015년 6월 5일
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일
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일
is this close to what you want?
>> strsplit( strjoin(x), ',')
ans =
'words' '10' '20' '30' 'more words'
  댓글 수: 7
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by