필터 지우기
필터 지우기

Concatenate the index i within the loop

조회 수: 2 (최근 30일)
Amanda Camarata
Amanda Camarata 2023년 12월 9일
댓글: Dyuman Joshi 2023년 12월 9일
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'}
.
  댓글 수: 2
Amanda Camarata
Amanda Camarata 2023년 12월 9일
I didn't know about this method. Thank you!
Star Strider
Star Strider 2023년 12월 9일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (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