Filter löschen
Filter löschen

Concatenate the index i within the loop

2 Ansichten (letzte 30 Tage)
Amanda Camarata
Amanda Camarata am 9 Dez. 2023
Kommentiert: Dyuman Joshi am 9 Dez. 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 Kommentar
Dyuman Joshi
Dyuman Joshi am 9 Dez. 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"

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 9 Dez. 2023
Bearbeitet: Star Strider am 9 Dez. 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 Kommentare
Amanda Camarata
Amanda Camarata am 9 Dez. 2023
I didn't know about this method. Thank you!
Star Strider
Star Strider am 9 Dez. 2023
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by