Borrar filtros
Borrar filtros

Concatenate the index i within the loop

2 visualizaciones (últimos 30 días)
Amanda Camarata
Amanda Camarata el 9 de Dic. de 2023
Comentada: Dyuman Joshi el 9 de Dic. de 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 comentario
Dyuman Joshi
Dyuman Joshi el 9 de Dic. de 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"

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 9 de Dic. de 2023
Editada: Star Strider el 9 de Dic. de 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 comentarios
Amanda Camarata
Amanda Camarata el 9 de Dic. de 2023
I didn't know about this method. Thank you!
Star Strider
Star Strider el 9 de Dic. de 2023
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by