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

71 次查看(过去 30 天)
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.

采纳的回答

Rishik Ramena
Rishik Ramena 2021-3-21
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 个评论
David Szwer
David Szwer 2021-3-22
I have extended that example a bit to make it a bit clearer how the tile order does or doesn't change as axes are added and deleted.
x = linspace(0,6);
y1 = 1*sin(pi*x/1);
y2 = 2*sin(pi*x/2);
y3 = 3*sin(pi*x/3);
y4 = 4*sin(pi*x/4);
y5 = 5*sin(pi*x/5);
y6 = 6*sin(pi*x/6);
y7 = 7*sin(pi*x/7);
tiledlayout('flow')
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
delete(nexttile(2)) % Graph 2 disappears.
pause
delete(nexttile(2)) % Nothing happens - not even a warning.
pause
delete(nexttile(4)) % Graph 4 disappears.
pause
delete(nexttile(1)) % Graph 1 disappears.
pause
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.
Rishik Ramena
Rishik Ramena 2021-3-22
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by