Concatenate the index i within the loop

2 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-12-9
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"

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2023-12-9
编辑:Star Strider 2023-12-9
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'}
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by