how can I move the legend in uiaxes?
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mario Schwarz
el 9 de En. de 2020
I am in App Designer. Creating an uiaxes and showing measurement curves. The positioning of the Legend is driving me crazy: Is it really not possible to move the legend via drag and drop? I cannot believe that this is not possible. Could anyone please help me out?
Thanks!
0 comentarios
Respuesta aceptada
Adam Danz
el 9 de En. de 2020
Editada: Adam Danz
el 10 de En. de 2020
"The positioning of the Legend is driving me crazy"
Here are two options.
Set legend location/position from within your code
In the plotting function that produces the legend, you can set the location or position property of the legend so that it's outside of your axes or within a less populated section of your axes.
legend(app.UIAxes, . . ., 'location', 'eastoutside')
Create an interactive menu option to change legend location
Follow these steps the create a dropdown menu option that allows the user to select a new legend location. This step-by-step demo is also attached.
1) When you produce the legend, store the legend handle within the app.UIAxes.UserData field so it can later be accessed.
% create plot
plot(app.UIAxes, magic(3))
lh = legend(app.UIAxes,'A','B','C');
app.UIAxes.UserData = lh;
2) From app designer Design View, add a menu bar item to your UIFigure.
3) Use the component browswer option to rename the menu item and add legend position options. This example uses NE for NorthEast, NW for NorthWest, etc....
4) Add a callback function using the "Add MenuSelectedFcn" option and assign that same callback function to each menu option using the "Select existing callback" option.
This will add a new function in Code View
5) The callback function will get the handle from the UIAxes.UserData field and it will change the legend loctation according to the UIMenu selection.
function NEMenuSelected(app, event)
% Get legend handle from UIAxes userdata
lh = app.UIAxes.UserData;
if isempty(lh)
% do nothing if legend handles isn't available
return
end
% Reposition legend according to selected menu item
switch upper(event.Source.Text)
case 'NE'
lh.Location = 'northeast';
case 'SE'
lh.Location = 'southeast';
case 'NW'
lh.Location = 'northwest';
case 'SW'
lh.Location = 'southwest';
otherwise
% This should never happen
error('Menu item not recognized.')
end
end
end
[addendum]
You could also add a 'Toggle' option that toggles on/off the visibility of the legend.
case 'TOGGLE'
if strcmpi(lh.Visible,'on')
lh.Visible = 'off';
else
lh.Visible = 'on';
end
Now you can move the legend by selecting a new location from the dropdown menu.
"Is it really not possible to move the legend via drag and drop"
Yes, it's really not possible (as of r2019b). App designer is still in development and new features are being rolled out with each Matlab release.
You can, however, pan the axes by using the Pan tool (shown below) so you can see the data behind the legend.
0 comentarios
Más respuestas (1)
Cris LaPierre
el 9 de En. de 2020
It is not possible to move the legend with your mouse.
The location is set using a name-value pair argument (see doc here). The command should be something like this.
legend(app.UIAxes,'Location','northeastoutside')
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!