How can I use a legend in App Designer to toggle line visibility on and off?

29 views (last 30 days)
In GUIDE or with 'figure', I can use the 'ItemHitFcn' callback of a 'legend' to toggle the visibility of a line on and off. This is shown in an example below:
However, when I try to set the 'ItemHitFcn' of a legend in App Designer or on a UIFigure, I get an error saying that that functionality is not supported:
Error using matlab.graphics.illustration.Legend/set.ItemHitFcn
Functionality not supported with figures created with the uifigure function. For more information, see Graphics Support in App Designer.
How can I use a legend in App Designer or UIFigures to toggle line visibility on and off?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Aug 2019
To toggle line visibility on and off, you can make use of the 'ButtonDownFcn' callback of the legend instead. Please see the below link for more information on this callback:
I have attached a sample app that demonstrates how to do this. In summary, the app does the following:
1. It has a UIAxes, and at startup it plots lines and a legend onto this UIAxes
2. It then sets the legend 'ButtonDownFcn' callback to be a custom function that is part of the App itself
3. In this custom function, we use the 'Event data' argument to determine where in the legend was clicked
4. Once we have where was clicked, we can use that to roughly map back to what line number in the legend that corresponds to.
5. We can get all the lines from 'app.UIAxes.Children', and then choose the appropriate line with the line number we got in the previous step.
6. Then, we can toggle the 'Visibility' property of that line to 'off' or 'on' accordingly
  3 Comments
Sebastian Engelsgaard
Sebastian Engelsgaard on 23 Apr 2021
This feature is really usable, but I have a question; How to set all plots to 'unclicked' in startup/initialize them as clicked?

Sign in to comment.

More Answers (1)

Sebastian Engelsgaard
Sebastian Engelsgaard on 26 Apr 2021
This feature is really usable, but I have a question; How to set all plots to 'unclicked' in startup / initialize them as clicked?

Tags

No tags entered yet.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!