Trying to create a scrollable uifigure but unsure how to reference positions

16 views (last 30 days)
I'm having a hard time understanding how MATLAB handles content that is larger than the size of a figure when the figure's scrollable property is true. Here's a toy example. I'll note from this example it seems that it is not possible to buffer space on the top (only on the bottom). I guess my mental model is something like a canvas size and a viewport, where the latter is smaller than the former. I'll note this is not how I normally think about MATLAB figures, but I'm wondering if it is possible to create this effect. Overall the goal is to support something like a scrollable web page.
Here's the code. Is there a better way of thinking of how scrollable interacts with the figure layout?
fig = uifigure('Position', [100 100 500 800]);
fig.Scrollable = 'on';
canvasWidth = 450;
canvasHeight = 3500; % much taller than visible; scrolling required
% Setting temporarily so that the uiaxes calls make sense.
%
%Does this actually happen? What are the limitations here?
%Should scrolling be enable after rendering/layout?
fig.Position = [1 1 canvasWidth canvasHeight];
% Number of axes
N = 10;
axHeight = 250;
axWidth = 400;
padding = 20;
ax = gobjects(N,1);
% Create axes stacked vertically
for i = 1:N
% Compute vertical position from top down
y = canvasHeight - 400 - i*(axHeight + padding);
ax(i) = uiaxes(fig, ...
'Position', [20, y, axWidth, axHeight]);
plot(ax(i),1:2*i)
title(ax(i), sprintf('Axes %d', i));
end
linkaxes(ax)
fig.Position = [100 100 500 800];

Accepted Answer

Aditya
Aditya 1 minute ago
Hi Jim,
MATLAB's scrollable figures scroll over the bounding box of their child components. To create extra padding (like a web page), you must add invisible components at the desired edges.
To add space at the top (or left), you must explicitly position a component (even an invisible one) at the desired top-left "corner" of your logical canvas
% Add a dummy (invisible) uilabel for padding at the top
topBuffer = 100; % pixels
dummy = uilabel(fig, 'Position', [1, canvasHeight-topBuffer, 1, 1], ...
'Text', '', 'Visible', 'off');
This ensures the scrollable region starts topBuffer pixels above your first axes.
You can refer to the following documentation for more details:

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!