how can I move the legend in uiaxes?

17 ビュー (過去 30 日間)
Mario Schwarz
Mario Schwarz 2020 年 1 月 9 日
編集済み: Adam Danz 2020 年 1 月 10 日
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!

採用された回答

Adam Danz
Adam Danz 2020 年 1 月 9 日
編集済み: Adam Danz 2020 年 1 月 10 日
"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.

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 1 月 9 日
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')
  1 件のコメント
Mario Schwarz
Mario Schwarz 2020 年 1 月 9 日
Thank you Cris,
moving the legend is possible with the plot function outside AppDesigner.
In my case i sometimes have hundreds of lines and sometimes only one. I have a check box to make the Legend invisible. So in case of having more lines and the legend is not visible, any 'outside' location is reserving huge amount of space and the graph itself gets shrinked.
So it would be very nice, if the user can drag and drop the Legend wherever he wants. In my opinion the Legend thing in Matlab has always been a pain in the ass. I am always programming small measurement analyis apps and need a interactive, flexible legend.
In the meanwhile the best solutions is choosing 'Location','best'.
Thanks for your answer.

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

カテゴリ

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