How to add strings to an array within a loop
显示 更早的评论
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!
采纳的回答
更多回答(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)
2 个评论
Sayan Banerjee
2020-11-18
Ameer Hamza
2020-11-18
Change the line to
legend_names = compose('Case\\_%d', 1:30)
类别
在 帮助中心 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!