Using String user input for dynamic number of legend in plot function

1 回表示 (過去 30 日間)
Romain Liechti
Romain Liechti 2019 年 3 月 29 日
コメント済み: Star Strider 2019 年 3 月 29 日
Hi, I'm trying to use the user input from input as a legend for graph in a plot function. I would like my function to use the string input from the user as legend.
for jj = 1:dim
legend2 = ['Enter a legend for curve ' num2str(jj) ': '];
LegendCell(jj) = input(legend2);
end
[...]
l = legend(LegendCell,'Location','southwest');
For now, no legend is added to the plot, and I already tried differents conversion from string to string cell, array etc...
When I do this outside of the function, it works fine, so my guess is that the problem is from the data coming from the "input"
Legend(1) = "Sine";
Legend(2) = "Cosine";
x = 0:0.1:10;
y = sin(x);
y2 = cos(x);
figure()
plot(x,y)
hold on
plot(x,y2)
legend(Legend)

採用された回答

Star Strider
Star Strider 2019 年 3 月 29 日
Change the input call to:
LegendCell{jj} = input(legend2,'s')
That worked (for one legend entry) when I tested it.
  2 件のコメント
Romain Liechti
Romain Liechti 2019 年 3 月 29 日
Thanks, it works, also for multiple graphs
Star Strider
Star Strider 2019 年 3 月 29 日
As always, my pleasure.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by