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 figureuitg = uitabgroup(uif, 'Position', [0 0 uif.Position(3:4)]); % Create a tab group within the UI figureuit = 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?
Best Answer