How to add strings to an array within a loop
조회 수: 24 (최근 30일)
이전 댓글 표시
Hello Team, I want to create an array of legend names for a plot. The final array should look like this:
legend_names = ["Case_1", "Case_2", Case_3", ......., "Case_30"]
So, I created a loop to create the variable names,
For ii=1:30
name = sprintf("Case_%d", ii); %%% to create the Case_X names
legend_names = append......?? %%% to create the array as mentioned before
end
Not sure how to construct the array in the loop.
Thanks for your help!
댓글 수: 0
채택된 답변
Cris LaPierre
2020년 11월 18일
If you are using strings, you can create this just using "+".
legend_names = "Case_" + string(1:5)
댓글 수: 2
Cris LaPierre
2020년 11월 18일
Set the tex interpreter to none.
legend(legend_names,'Interpreter',"none")
추가 답변 (1개)
Ameer Hamza
2020년 11월 18일
편집: Ameer Hamza
2020년 11월 18일
Easier is to use compose()
legend_names = compose('Case_%d', 1:30)
It create a cell array which can be directly use with legend()
legend(legend_names)
You can also directly create a string array
legend_names = compose("Case_%d", 1:30)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!