Missing context menu for "axes" contained in "uifigure" parent

조회 수: 7 (최근 30일)
Alexander May
Alexander May 2020년 4월 22일
답변: Deepak 2024년 11월 11일 9:35
Hello,
I would like to enable the default context menu functionality for axes objects inside a uifigure parent (or a panel within a uifigure).
For an axes within a figure object, the context menu appears when the user has selected one of the axes toolbar buttons and subsequently right clicks on the axes. However, for an axes within a uifigure object, the context menu does not appear. See code below for a guided example of this behavior difference.
Is it possible to enable this default context menu functionality for an axes within a uifigure hierarchy?
% Compare default context menu functionality with Axes objects created in a
% figure and uifigure parent.
% Figure parent
h = figure;
ax = axes(h);
title(ax, 'This is a ''figure'' with an ''axes'' child')
disp('A figure has been created with an axes child.')
disp('Select a toolbar interaction button, such as the Zoom button.')
disp('Right click on the axes and observe the context menu which appears.')
input('Press enter to continue');
disp('---')
% UIFigure parent
hUI = uifigure;
axUI = axes(hUI);
title(axUI, 'This is a ''uifigure'' with an ''axes'' child')
disp('A uifigure has been created with an axes child.')
disp('Select a toolbar interaction button, such as the Zoom button.')
disp('Note that no context menu appears.')
disp('How can one enable the default context menus for axes contained in a uifigure?')
input('Press enter to close figures');
delete(h)
delete(hUI)
  댓글 수: 1
Luc
Luc 2021년 3월 15일
I had the same problem and noticed that this will do the trick:
app.UIAxesZoom=zoom(app.UIAxes);
setAllowAxesZoom(app.UIAxesZoom,app.UIAxes,1);

댓글을 달려면 로그인하십시오.

답변 (1개)

Deepak
Deepak 2024년 11월 11일 9:35
In MATLAB, uifigure and figure objects have different underlying architectures, which is why we notice differences in behaviour, such as the context menu functionality. The uifigure is based on a newer UI framework that supports modern app building, but it doesn't support all features available in the traditional figure.
To enable a context menu for axes within a uifigure, we need to manually create a uicontextmenu and associate it with the axes. We can use uicontextmenu to create a context menu that can be attached to UI components within a uifigure. By utilizing uimenu, we can add items to this context menu, with each item capable of being assigned a specific callback function through the MenuSelectedFcn property. Finally, the ContextMenu property of the axes object allows us to assign the created context menu to the axes, enabling customized right-click interactions.
Here is the sample MATLAB code to achieve the same:
% Create a UIFigure
hUI = uifigure;
axUI = axes(hUI);
title(axUI, 'This is a ''uifigure'' with an ''axes'' child')
% Create a UIContextMenu
cm = uicontextmenu(hUI);
% Define menu items
m1 = uimenu(cm, 'Text', 'Zoom In', 'MenuSelectedFcn', @(src,event) zoom(axUI, 'on'));
m2 = uimenu(cm, 'Text', 'Zoom Out', 'MenuSelectedFcn', @(src,event) zoom(axUI, 'out'));
% Assign the context menu to the axes
axUI.ContextMenu = cm;
disp('A uifigure has been created with an axes child.')
disp('Right click on the axes to see the custom context menu.')
input('Press enter to close the figure');
delete(hUI)
Please see the attached documentation for the functions referenced:
I hope this assists in resolving the issue.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by