Cannot use WindowButtonDownFcn and zoom in the same app in newer Matlab version

6 次查看(过去 30 天)
I have a rather complex app which is giving me a small problem when switching from Matlab 2019a to 2021b. The relevant aspects of the app are:
  • If the user clicks in the figure window, a certain function is called (which checks whether the click was within some axes and then responds accordingly).
  • For one axes object, the zoom tool should be enabled by default.
I have a startupFcn that initializes these things:
% Code that executes after component creation
function startupFcn(app, mainapp)
% setup most of the figure contents
VisualizationStartup(app, mainapp);
% setup the function called upon click in figure
app.UIFigure.WindowButtonDownFcn = createCallbackFcn(app, @VisualizationClickOnPlot, true);
% enable zoom on plot_tt axes
zoom(app.plot_tt, 'on');
end
In Matlab 2019a, this works as desired. However, when I open the app in Matlab 2021b, the zoom on plot_tt is enabled but the WindowButtonDownFcn does not work. If I set a breakpoint in the VisualizationClickOnPlot function, it turns out that this function is not called at all upon clicking in the figure (whereas it is called in 2019a).
If I remove the line that enables the zoom, the WindowButtonDownFcn works properly in both versions, but I want the zoom as well.
I tried switching the two lines around, to first enable the zoom and then define the WindowButtonDownFcn. In 2019a this makes no difference and everything still works; in 2021b the WindowButtonDownFcn still does not work while the zoom does, but then I get a warning: “Setting the "WindowButtonDownFcn" property is not permitted while this mode is active.”
So it seems like zoom and WindowButtonDownFcn are compatible in 2019a but mutually exclusive in 2021a (with zoom overruling WindowButtonDownFcn irrespective of the order in which they are set up). Does anyone know if this is true, and is there a way to still use both functionalities in the same app?

回答(1 个)

Satyam
Satyam 2025-6-26
I faced a similar problem in the past. Assigning a 'WindowButtonDownFcn' callback to a 'uifigure' in App Designer overrides the default mouse button behaviour across the entire app window. This can interfere with built-in interactive tools like zooming and panning.
In MATLAB 2021b, the zoom interaction mode takes control of mouse event handling and suppresses 'WindowButtonDownFcn' functionality. If you want to support both your custom behaviour and the default zoom/pan functionality, you’ll need to add logic within your callback to distinguish between user interactions and decide when to execute your custom code versus letting the default behaviour proceed.
There is a possible workaround to remove the warning: “Setting the WindowButtonDownFcn property is not permitted while this mode is active.” You can turn off any active mode before setting the callbacks for their mode. Here is a code snippet demonstrating this workaround:
function startupFcn(app, mainapp)
% setup most of the figure contents
VisualizationStartup(app, mainapp);
zoom(app.plot_tt, 'off');
% setup the function called upon click in figure
app.UIFigure.WindowButtonDownFcn = createCallbackFcn(app, @VisualizationClickOnPlot, true);
% enable zoom on plot_tt axes
zoom(app.plot_tt, 'on');
end
I hope it solves your query.

类别

Help CenterFile Exchange 中查找有关 Data Exploration 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by