How I could convert matrix (double) to cell array of string

I have the following matrix and I want to convert it in cell array of string.
Please help me with this issue.
A=[-1; -3; -5; -5; -6; -7; -9; -3; -7; -9; -8; -9; -10]

 채택된 답변

Walter Roberson
Walter Roberson 2016년 5월 31일
A_cell = cellstr(str2num(A));

댓글 수: 5

Maryam Hamrahi
Maryam Hamrahi 2016년 5월 31일
편집: Maryam Hamrahi 2016년 5월 31일
Many thanks for the answer. I used your code and I got the following error.
Error using str2num (line 32)
Requires string or character array input.
Error in (line 2) A_cell = cellstr(str2num(A))
A_cell = cellstr(num2str(A));
I don't know why I keep getting that reversed...
Thank you very much.
Irina Ciortan
Irina Ciortan 2021년 4월 14일
편집: Irina Ciortan 2021년 4월 14일
This is not correct. Str2num converts strings to numeric format, but the question was the other way around. The correct and best answer should be https://se.mathworks.com/matlabcentral/answers/286544-how-i-could-convert-matrix-double-to-cell-array-of-string#answer_331847
Look at the correction posted in May 2016

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

추가 답변 (5개)

Iskander
Iskander 2017년 11월 30일
편집: Iskander 2017년 11월 30일
Use undocumented function:
sprintfc('%d',A)

댓글 수: 5

This is the best implementation I have seen. Very nice!
This needs to be higher ! Thanks !
Awesome! Thanks for this extremely concise method. As Patrick said, this needs to be higher!
This functionality is basically the same as the documented function compose. Try
compose('%d',A)
Note: compose requires R2016b or later.

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

Benny Abramovsky
Benny Abramovsky 2018년 8월 7일
This one worked for me:
strsplit(num2str(A))

댓글 수: 3

but it'll work only on row vector
This can work for a general matrix using
strsplit(num2str(A(:)'))
and reshaping as necessary.
This is correct answer and worked for me.

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

Karolis Poskus
Karolis Poskus 2020년 10월 14일
Using one function:
compose('%g',A)

댓글 수: 3

best answer
This, that you say is "best answer", is the same solution that was posted 23 months earlier at https://www.mathworks.com/matlabcentral/answers/286544-how-i-could-convert-matrix-double-to-cell-array-of-string#comment_637933
Also, at the time of the original question, compose() did not exist: the original question was May 2016, which was R2016a, but compose() was introduced as part of the string operations in R2016b.

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

Rubén Vázquez Amos
Rubén Vázquez Amos 2024년 2월 27일

1 개 추천

Wouldn't string(A) work?

댓글 수: 3

DGM
DGM 2024년 2월 27일
편집: DGM 2024년 2월 27일
I suppose it does, but to be fair, that wouldn't have been an option in early 2016.
I did test it and it worked, but wasn't sure about 2016 compatibility so I figured I'd put it as a tentative answer.
DGM
DGM 2024년 2월 27일
편집: DGM 2024년 2월 27일
As far as I know, string() was introduced in R2016b, but I don't know that it's early behavior was the same as it is today. I think it's safe to say the legacy options don't matter to most readers, and they'd probably be fine with your answer. The only reason I bring it up is to explain why it hadn't been mentioned at the time the question was asked.

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

WinCento99
WinCento99 2021년 7월 13일
Hi all,
If we define A as a matrix
A = [1 , 2 ; 3 , 4]
And we want to create a cell string matrix, do we do the following?
B = cellstr(num2str(A))
for i = 1:length(B)
C(i,:) = strsplit(B{i,1}) ;
end
Is there a way to ignore the loop?

댓글 수: 1

A = [1 , 2 ; 3 , 4]
A = 2×2
1 2 3 4
B = cellstr(string(A))
B = 2×2 cell array
{'1'} {'2'} {'3'} {'4'}

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

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2016년 5월 31일

편집:

DGM
2024년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by