Effacer les filtres
Effacer les filtres

Concatenate the index i within the loop

2 vues (au cours des 30 derniers jours)
Amanda Camarata
Amanda Camarata le 9 Déc 2023
Commenté : Dyuman Joshi le 9 Déc 2023
I'm trying to create a list for my legend without hard coding it, but I'm having trouble figure out how to concatenate the number associated with 'i' in my loop.
I'm hoping I'll end up with a list of strings = ['Node 1','Node 2','Node 3'....]
num_labels = 10;
num_labels = 10
labels = zeros(1,num_labels);
for i = 1:num_labels
labels(1,i) = strcat('Node',i);
end
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-5.
  1 commentaire
Dyuman Joshi
Dyuman Joshi le 9 Déc 2023
A method using strings -
num = 10;
labels = "Node " + (1:num)
labels = 1×10 string array
"Node 1" "Node 2" "Node 3" "Node 4" "Node 5" "Node 6" "Node 7" "Node 8" "Node 9" "Node 10"

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 9 Déc 2023
Modifié(e) : Star Strider le 9 Déc 2023
Use the compose function —
num_labels = 10;
labels = compose('Node %d',1:num_labels)
labels = 1×10 cell array
{'Node 1'} {'Node 2'} {'Node 3'} {'Node 4'} {'Node 5'} {'Node 6'} {'Node 7'} {'Node 8'} {'Node 9'} {'Node 10'}
.
  2 commentaires
Amanda Camarata
Amanda Camarata le 9 Déc 2023
I didn't know about this method. Thank you!
Star Strider
Star Strider le 9 Déc 2023
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by