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

76 ビュー (過去 30 日間)
David Szwer
David Szwer 2021 年 3 月 16 日
コメント済み: Rishik Ramena 2021 年 3 月 22 日
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 Center および File Exchange2-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