Can I remove axes from a tiledlayout, and have it reflow?

72 views (last 30 days)
Suppose I create a flowing tiledlayout
tiledlayout('flow')
and add a few plots with
nexttile()
The layout automatically rearranges the tiles to fit each new axes. But now suppose I want to remove one of my old axes to make more room for the rest? How can I remove an axes from a the tiledlayout, so that it rearranges and resizes the remaining axes?
Obviously I could save each axes in an array as I create it, and then re-create the tiledlayout from scratch, but maybe there's an easier way.

Accepted Answer

Rishik Ramena
Rishik Ramena on 21 Mar 2021
You can index the tiles using the nexttile(tilelocation) format. Then use the delete(gca) to delete any previously created tile axes. Have a look at the example below.
x = linspace(0,30);
y1 = sin(x/2);
y2 = sin(x/3);
y3 = sin(x/4);
% Plot into first tile three times
tiledlayout('flow')
nexttile
plot(x,y1)
nexttile;
plot(x,y2)
nexttile
plot(x,y3)
delete(nexttile(2))
  3 Comments
Rishik Ramena
Rishik Ramena on 22 Mar 2021
nexttile
plot(x,y1)
pause % Wait for the user to press any key in the Command Window.
nexttile;
plot(x,y2)
pause
nexttile
plot(x,y3)
pause
nexttile
plot(x,y4)
pause
%% four tiles are created till now
delete(nexttile(2)) % Graph 2 disappears.
pause
delete(nexttile(2)) % Nothing happens - not even a warning.
%% This happens because the axes object indexed by nexttile(2) no longer exists hence nothing is deleted.
pause
delete(nexttile(4)) % Graph 4 disappears.
pause
delete(nexttile(1)) % Graph 1 disappears.
pause
%% Till here only the axes object indexed by nexttile(3) is present in the current figure and indices 1,2 and 4 are unoccupied
%% these plots will take up the vacant locations in the current figure which are indexed by 1,2 and 4
nexttile
plot(x,y5) % Graph 5 appears in position *1*.
pause
nexttile
plot(x,y6) % Graph 6 appears in position *2*.
pause
nexttile
plot(x,y7) % Graph 7 appears in position *4*.
pause
delete(nexttile(4)) % Graph *7* disappears.
pause
delete(nexttile(3)) % Graph 3 disappears.
pause
delete(nexttile(1)) % Graph *5* disappears.
Hope this clears up your understanding.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by