MATLAB Answers

Cheating with subplots, AutoResizeChildren

7 views (last 30 days)
Ted Londner
Ted Londner on 3 Nov 2020
Commented: Ted Londner on 4 Nov 2020
Subplots can only be created within parents where the AutoResizeChildren property is not set to 'on'. This can be problematic when you are attempting to create a subplot within a uitab object, as you might when designing an app. For example, this code will throw an error:
uif = uifigure; % Create a UI figure
uitg = uitabgroup(uif, 'Position', [0 0 uif.Position(3:4)]); % Create a tab group within the UI figure
uit = uitab(uitg); % Create a tab within the tab group
subplot(2, 2, 1, 'Parent', uit); % Attempt to generate a subplot within the tab. An error will be thrown.
subplot(2, 2, 2, 'Parent', uit);
subplot(2, 2, 3, 'Parent', uit);
subplot(2, 2, 4, 'Parent', uit);
Note that the error will be thrown on line 325 of subplot.m (version 2020a).
However, you can circumvent this error by creating your subplots in a separate figure (a regular figure, not a uifigure) and then moving them into your uitab object by changing their parents. For example, this code will not throw an error:
dfig = figure; % Create a dummy figure
uif = uifigure; % Create a UI figure
uitg = uitabgroup(uif, 'Position', [0 0 uif.Position(3:4)]); % Create a tab group within the UI figure
uit = uitab(uitg); % Create a tab within the tab group
% Generate subplots within the dummy figure then immediately move them into the UI figure
ax = subplot(2, 2, 1, 'Parent', dfig);
ax.Parent = uit;
ax = subplot(2, 2, 2, 'Parent', dfig);
ax.Parent = uit;
ax = subplot(2, 2, 3, 'Parent', dfig);
ax.Parent = uit;
ax = subplot(2, 2, 4, 'Parent', dfig);
ax.Parent = uit;
% Close the dummy figure
close(dfig);
I'd like to better understand what's going on here. The subplots generated by the second batch of code written above seem to work normally and they resize as you change the size of the uifigure window. If this is so, why is there a condition placed on the AutoResizeChildren property within subplot.m? Is there some hidden vulnerability to doing things this way?

  0 Comments

Sign in to comment.

Accepted Answer

Dave B
Dave B on 4 Nov 2020
Hi Ted:
Yes, this is a neat workaround, but it does indeed cause a problem.
You can think of the subplot function as doing two things: initial layout of the axes, and keeping the axes positioned as their size is automatically changed. For example, in your code snippet if you add:
xlabel(ax,'LABEL','FontSize',30)
You'll see that the label gets cut off. If you add this label without the reparenting, you'll see that the axes you add the label to gets its size adjusted, and all of the other axes get their position adjusted to line up. Note that there are several things you might change on an axes that would cause it to resize, xlabel is just one example. In short, subplot continues to manage the position of the axes without the reparenting, but you lose this behavior with reparenting.
As you're using 2020a, you might consider trying tiledlayout/nexttile (introduced in R2019b) in place of subplot. This works well in uifigures, and has some great new features for controlling your layout.
uif = uifigure;
uitg = uitabgroup(uif, 'Position', [0 0 uif.Position(3:4)]);
uit = uitab(uitg);
t=tiledlayout(uit,2,2); % Create a tiledlayout in the uitab
for i = 1:4
nexttile(t); % Add an axes to the next available tile in the layout.
end
Documentation links:

  1 Comment

Ted Londner
Ted Londner on 4 Nov 2020
Dave,
Thank you! This answers my question.
Ted

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!