How I could convert matrix (double) to cell array of string
105 次查看(过去 30 天)
显示 更早的评论
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]
0 个评论
采纳的回答
Walter Roberson
2016-5-31
A_cell = cellstr(str2num(A));
5 个评论
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
更多回答(5 个)
Iskander
2017-11-30
编辑:Iskander
2017-11-30
Use undocumented function:
sprintfc('%d',A)
5 个评论
Neil Patel
2018-11-15
This functionality is basically the same as the documented function compose. Try
compose('%d',A)
Benny Abramovsky
2018-8-7
This one worked for me:
strsplit(num2str(A))
3 个评论
Syed Hussain
2018-9-12
This can work for a general matrix using
strsplit(num2str(A(:)'))
and reshaping as necessary.
Karolis Poskus
2020-10-14
Using one function:
compose('%g',A)
3 个评论
Walter Roberson
2021-4-10
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
2024-2-27
Wouldn't string(A) work?
3 个评论
Rubén Vázquez Amos
2024-2-27
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
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
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 个评论
另请参阅
类别
在 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!