Create legend within a for loop but with changing size of plot handles

1 回表示 (過去 30 日間)
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017 年 6 月 29 日
コメント済み: Walter Roberson 2017 年 6 月 29 日
Hi all,
I have a plot within a for loop, plotting 22 line objects on the first run (i=1) and 28 line objects on the second run (i=2). I would like to have a legend that has the a line object from the first run and a line object from the second run.
Sounds simple, however as the handle sizes are changing, i cant seem to be able to save them in a cell array so that I can call them afterwards e.g.:
for i=1:2
hold on
p(:,i) = {plot(...22 line objects for i=1 and 28 for i=2...)} ;
hold off
end
legend([p(1,1) p(1,2)],'first run','second run')
The result is a cellarray p={[epmty] [28x1 line objects]} which means that for some reason the handles from the first run are not being saved.
Thanks for your help in advance,
KMT.
  1 件のコメント
Adam
Adam 2017 年 6 月 29 日
p{i} = plot(...)
would seem a more obvious way to fill a cell array, though whether this will solve the problem or not I don't know. I don't see why your first element would be empty other than because the plotting of 22 objects is not actually returning any handles, which will be the same whatever assignment syntax you use.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 6 月 29 日
for i=1:2
hold on
p{i} = {plot(...22 line objects for i=1 and 28 for i=2...)} ;
hold off
end
legend([p{1}(1), p{2}(1)], 'first run', 'second run')
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 6 月 29 日
Difficult to say without the actual code.
Walter Roberson
Walter Roberson 2017 年 6 月 29 日
In your line
p{i} = {plot(...22 line objects for i=1 and 28 for i=2...)} ;
do you mean that you literally have plot() called within {} ??

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by