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

115 vues (au cours des 30 derniers jours)
Maryam Hamrahi
Maryam Hamrahi le 31 Mai 2016
Modifié(e) : DGM le 27 Fév 2024
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]

Réponse acceptée

Walter Roberson
Walter Roberson le 31 Mai 2016
A_cell = cellstr(str2num(A));
  5 commentaires
Irina Ciortan
Irina Ciortan le 14 Avr 2021
Modifié(e) : Irina Ciortan le 14 Avr 2021
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
Walter Roberson
Walter Roberson le 14 Avr 2021
Look at the correction posted in May 2016

Connectez-vous pour commenter.

Plus de réponses (5)

Iskander
Iskander le 30 Nov 2017
Modifié(e) : Iskander le 30 Nov 2017
Use undocumented function:
sprintfc('%d',A)
  5 commentaires
Neil Patel
Neil Patel le 15 Nov 2018
This functionality is basically the same as the documented function compose. Try
compose('%d',A)
Walter Roberson
Walter Roberson le 16 Nov 2018
Note: compose requires R2016b or later.

Connectez-vous pour commenter.


Benny Abramovsky
Benny Abramovsky le 7 Août 2018
This one worked for me:
strsplit(num2str(A))
  3 commentaires
Syed Hussain
Syed Hussain le 12 Sep 2018
This can work for a general matrix using
strsplit(num2str(A(:)'))
and reshaping as necessary.
Irina Ciortan
Irina Ciortan le 14 Avr 2021
This is correct answer and worked for me.

Connectez-vous pour commenter.


Karolis Poskus
Karolis Poskus le 14 Oct 2020
Using one function:
compose('%g',A)
  3 commentaires
Kyuhwa Lee
Kyuhwa Lee le 10 Avr 2021
best answer
Walter Roberson
Walter Roberson le 10 Avr 2021
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.

Connectez-vous pour commenter.


Rubén Vázquez Amos
Rubén Vázquez Amos le 27 Fév 2024
Wouldn't string(A) work?
  3 commentaires
Rubén Vázquez Amos
Rubén Vázquez Amos le 27 Fév 2024
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 le 27 Fév 2024
Modifié(e) : DGM le 27 Fév 2024
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.

Connectez-vous pour commenter.


WinCento99
WinCento99 le 13 Juil 2021
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 commentaire
Walter Roberson
Walter Roberson le 13 Juil 2021
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'}

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by