- Undocumented Matlab - https://undocumentedmatlab.com -
PlotEdit context-menu customization
Posted By Yair Altman On December 13, 2017 | No Comments
Last week, a Matlab user asked [1] whether it is possible to customize the context (right-click) menu that is presented in plot-edit mode. This menu is displayed by clicking the plot-edit (arrow) icon on the standard Matlab figure toolbar, then right-clicking any graphic/GUI element in the figure. Unfortunately, it seems that this context menu is only created the first time that a user right-clicks in plot-edit mode – it is not accessible before then, and so it seems impossible to customize the menu before it is presented to the user the first time.
java.awt.Robot.mouseMove()
[3] method).java.awt.Robot
[4] class (as discussed in this blog [3] back in 2010)Robot
object
% Create an initial figure / axes for demostration purpose
fig = figure('MenuBar','none','Toolbar','figure');
plot(1:5); drawnow;
% Enter plot-edit mode temporarily
plotedit(fig,'on'); drawnow
% Preserve the current mouse pointer location
oldPos = get(0,'PointerLocation');
% Move the mouse pointer to within the axes boundary
% ref: https://undocumentedmatlab.com/blog/undocumented-mouse-pointer-functions
figPos = getpixelposition(fig); % figure position
axPos = getpixelposition(gca,1); % axes position
figure(fig); % ensure that the figure is in focus
newPos = figPos(1:2) + axPos(1:2) + axPos(3:4)/4; % new pointer position
set(0,'PointerLocation',newPos); % alternatives: moveptr(), java.awt.Robot.mouseMove()
% Simulate a right-click using Java robot
% ref: https://undocumentedmatlab.com/blog/gui-automation-robot
robot = java.awt.Robot;
robot.mousePress (java.awt.event.InputEvent.BUTTON3_MASK); pause(0.1)
robot.mouseRelease(java.awt.event.InputEvent.BUTTON3_MASK); pause(0.1)
% Modify the menu item
hMenuItem = findall(fig,'Label','Clear Axes');
if ~isempty(hMenuItem)
label = 'Undocumented Matlab';
callback = 'web(''https://undocumentedmatlab.com'',''-browser'');';
set(hMenuItem, 'Label',label, 'Callback',callback);
end
% Hide the context menu by simulating a left-click slightly offset
set(0,'PointerLocation',newPos+[-2,2]); % 2 pixels up-and-left
pause(0.1)
robot.mousePress (java.awt.event.InputEvent.BUTTON1_MASK); pause(0.1)
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK); pause(0.1)
% Exit plot-edit mode
plotedit(fig,'off'); drawnow
% Restore the mouse pointer to its previous location
set(0,'PointerLocation',oldPos);
In this code, I sprinkled a few pauses at several locations, to ensure that everything has time to fully render. Different pause values, or perhaps no pause at all, may be needed on your specific system.
Modifying the default context-menu shown in plot-edit mode may perhaps be an uncommon use-case. But the technique that I demonstrated above – of using a combination of Matlab and Java Robot
commands to automate a certain animation – can well be used in many other use-cases where we cannot easily access the underlying code. For example, when the internal code is encoded/encrypted, or when a certain functionality (such as the plot-edit context-menu) is created on-the-fly.
If you have encountered a similar use-case where such automated animations can be used effectively, please add a comment below.
Categories: Figure window, GUI, Handle graphics, Java, Low risk of breaking in future versions, Undocumented feature
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/plotedit-context-menu-customization
URLs in this post:
[1] a Matlab user asked: https://www.mathworks.com/matlabcentral/answers/370545-prompt-user-before-clearing-axis-when-using-the-clear-axes-context-menu
[2] moveptr: http://undocumentedmatlab.com/blog/undocumented-mouse-pointer-functions
[3] java.awt.Robot.mouseMove()
: http://undocumentedmatlab.com/blog/gui-automation-robot
[4] java.awt.Robot
: https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
[5] Adding a context-menu to a uitree : https://undocumentedmatlab.com/articles/adding-context-menu-to-uitree
[6] Context-Sensitive Help : https://undocumentedmatlab.com/articles/context-sensitive-help
[7] Customizing Workspace context-menu : https://undocumentedmatlab.com/articles/customizing-workspace-context-menu
[8] Uitable customization report : https://undocumentedmatlab.com/articles/uitable-customization-report
[9] Matlab toolstrip – part 3 (basic customization) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization
[10] Plot legend customization : https://undocumentedmatlab.com/articles/plot-legend-customization
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.