Graph adjust controls play peekaboo

조회 수: 1 (최근 30일)
Alessandro Livi
Alessandro Livi 2024년 7월 11일
답변: Ruchika Parag 2024년 7월 14일
My App Designer created app has a graph on it. My users want to adjust the horizontal axis (zoom in horizontally only)
I found where I can set that with a right click but the control things in the upper left corner are disappearing when ever the mouse approaches. Can't get there to grab one fast enough most of the time. Make them disappear when the mouse is NOT there and appear when it is please.

답변 (1개)

Ruchika Parag
Ruchika Parag 2024년 7월 14일
Hi Alessandro, for users to be able to adjust the horizontal axis in your app, you can programmatically control the appearance and behavior of the axes toolbar. You can try the following:
  1. Programmatically Control the Axes Limits: Add a callback function to control the horizontal zoom.
  2. Show/Hide Toolbar Based on Mouse Movement: Adjust the toolbar visibility based on the mouse position.
Please try the following steps for the same:
1. Add UI Controls for Horizontal Zoom
Add buttons or a slider in your app to control the horizontal zoom. For example, add two buttons "Zoom In" and "Zoom Out".
2. Add Callback Functions for Zoom Controls
Add callback functions for the zoom controls to adjust the XLim property of the axes.
% Zoom In Button Callback
function ZoomInButtonPushed(app, event)
ax = app.UIAxes; % Replace with your actual axes name
xlim = ax.XLim;
center = mean(xlim);
range = diff(xlim) / 2;
newXLim = [center - range/2, center + range/2];
ax.XLim = newXLim;
end
% Zoom Out Button Callback
function ZoomOutButtonPushed(app, event)
ax = app.UIAxes; % Replace with your actual axes name
xlim = ax.XLim;
center = mean(xlim);
range = diff(xlim) * 2;
newXLim = [center - range/2, center + range/2];
ax.XLim = newXLim;
end
3. Control Toolbar Visibility Based on Mouse Movement
You can use the 'WindowButtonMotionFcn' property of the figure to detect mouse movement and show/hide the toolbar accordingly.
% In the startup function of your app
function startupFcn(app)
app.UIFigure.WindowButtonMotionFcn = @(src, event) detectMouseMovement(app);
end
% Function to detect mouse movement
function detectMouseMovement(app)
ax = app.UIAxes; % Replace with your actual axes name
currentPoint = app.UIFigure.CurrentPoint;
axPosition = ax.Position;
figPosition = app.UIFigure.Position;
% Calculate the position of the axes in figure coordinates
axLeft = figPosition(1) + axPosition(1) * figPosition(3);
axBottom = figPosition(2) + axPosition(2) * figPosition(4);
axWidth = axPosition(3) * figPosition(3);
axHeight = axPosition(4) * figPosition(4);
% Check if the mouse is over the axes
if currentPoint(1) >= axLeft && currentPoint(1) <= (axLeft + axWidth) && ...
currentPoint(2) >= axBottom && currentPoint(2) <= (axBottom + axHeight)
% Show toolbar
ax.Toolbar.Visible = 'on';
else
% Hide toolbar
ax.Toolbar.Visible = 'off';
end
end
By following these steps, you should be able to provide your users with the ability to zoom in horizontally and control the visibility of the axes toolbar based on mouse movement. Hope this helps!

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by