Adding a legend from a loop into one element

8 ビュー (過去 30 日間)
Camille Fong
Camille Fong 2018 年 1 月 11 日
回答済み: Jan 2018 年 1 月 12 日
Hi, I created a loop (p4) and would like to find a way to combine them into one element so that when I click on p4 in the legend this element disappears (visibility off). See below for the code.
for k=1:size(A,1)
p4 = plot3([A(k,1),B(k,1)],[A(k,2),B(k,2)],[A(k,3),B(k,3)],'b');
end
...
L = legend([p1 p2 p3 p4 S p5 p7 p9],{'p1','p2','p3','p4','S','p5','p7','p9'},'location','Best');
L.ItemHitFcn = @hitcallback_ex1;
function hitcallback_ex1(src,evnt)
if strcmp(evnt.Peer.Visible,'on') evnt.Peer.Visible = 'off'; else evnt.Peer.Visible = 'on'; end
end
end
At the moment, nothing happens when I click on p4. Thank you in advance!
  3 件のコメント
Jos (10584)
Jos (10584) 2018 年 1 月 11 日
What are the undefined handles p1 p2 p3 S p5 p6 p9 ? Don't you want to use
p(k) = ...
legend(p, ...
Camille Fong
Camille Fong 2018 年 1 月 11 日
Hi Jan, Sorry if my question was not clear. I didn't copy paste my whole code.
My loop is as follow
for k=1:size(A,1)
p4 = plot3([A(k,1),B(k,1)],[A(k,2),B(k,2)],[A(k,3),B(k,3)],'b');
end
end
L = legend([p4],{'p4'},'location','Best');
L.ItemHitFcn = @hitcallback_ex1;
I added a function at the end of my code to allow the user to click on the legend to put it on or off the visibility of the loop.
The function is as followed :
function hitcallback_ex1(src,evnt)
if strcmp(evnt.Peer.Visible,'on')
evnt.Peer.Visible = 'off';
else
evnt.Peer.Visible = 'on';
end
end
end
I want to be able to click on the legend p4, and turn off the visibility of the loop (see attachment). At the moment, nothing happens when I clicked on p4. I hope it is more clear now.

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

回答 (1 件)

Jan
Jan 2018 年 1 月 12 日
I'm not sure, but maybe this helps:
L = legend(p4, {'p4'},'location','Best');
L.ItemHitFcn = {@hitcallback_ex, p4};
function hitcallback_ex1(src, evnt, LineH)
if strcmp(LineH.Visible, 'on')
LineH.Visible = 'off';
else
LineH.Visible = 'on';
end
end
Do you know FEX: ClickableLegend? It might be useful also.

カテゴリ

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