How to add numeric values to cell array?

40 vues (au cours des 30 derniers jours)
Souarv De
Souarv De le 7 Avr 2021
Modifié(e) : Souarv De le 8 Avr 2021
names = {'A' 'B' 'C' 'D' 'E'};
val = [0;1.5;0.5;0.5;1.5]
I want my output as folows
A 0
B 1.5
C 0.5
D 0.5
E 1.5

Réponse acceptée

Star Strider
Star Strider le 7 Avr 2021
Try this:
names = {'A' 'B' 'C' 'D' 'E'};
val = [0;1.5;0.5;0.5;1.5];
C = {[names{:}].' val}
C1 = C{:,1}
C2 = C{:,2}
TC = table(C1,C2)
.
  6 commentaires
Star Strider
Star Strider le 8 Avr 2021
Stephen — Thank you!
Another option is to replace them with string variables:
names = {"A1" "B1" "C1" "D1" "E1"};
.
Souarv De
Souarv De le 8 Avr 2021
Modifié(e) : Souarv De le 8 Avr 2021

Connectez-vous pour commenter.

Plus de réponses (1)

David Hill
David Hill le 7 Avr 2021
for k=1:length(names)
names{k}=[names{k},' ',num2str(val(k))];
end
  1 commentaire
Souarv De
Souarv De le 7 Avr 2021
@David Hill It is ok but in your code the output becomes again a cell type and both values are merged together as individual enitity of cell.

Connectez-vous pour commenter.

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by