Change GridSize of existing non empty TiledChartLayout object
이전 댓글 표시
Hello all,
I have a question regarding managing tiles layout.
I would like to change the organization of the layout after the tiles have already been created (for instance, for adding or removing a tile).
I thought I could just modify the GridSize property of the TiledChartLayout object after getting its handle.
However, when I try this approach, I get:
"Unable to set GridSize when TiledChartLayout is not empty"
So, it is unclear to me how to actually manage this situation.
Dummy example:
figure;
tl=tiledlayout(1,1); %first step-just one plot
nexttile(1);
plot([1 2 3],[4 5 6],'-o');
Imagine that now, after creating the figure, I would like to add a second tile, organising tiles in 2 rows and one column.
I was thinking I could simply do:
tl.GridSize=[2 1];
nexttile(2)
plot([1 2 3],-[4 5 6],'-s');
but it is not working. I get an error when trying to change the grid size (tl.GridSize=[2 1])
Unable to set GridSize when TiledChartLayout is not empty
If just try to add a new tile without trying to change the grid size (i.e. using directly nexttile(2)), I get the error:
Error using nexttile
The tile does not fit in the layout.
Any suggestion how to manage this?
Thank you,
Gabriele
채택된 답변
추가 답변 (2개)
Gabriele
2021년 6월 7일
댓글 수: 1
Adam Danz
2021년 6월 7일
That approach is fine, too. Just don't accidentally over-write the original figure by saving it (assuming the figure was opened). To reduce the chances of that happening you could close the original figure after the loop or, better yet, use onCleanup to close the figure in the event of an error. You might even want to open the figure in invisible mode using openfig(___,visibility).
Scott MacKenzie
2021년 6월 6일
Try using flow for the grid size...
tiledlayout('flow');
nexttile;
plot([1 2 3],[4 5 6],'-ob');
pause(2);
nexttile;
plot([2 1 3]',[4 5 6]','-dr');
First plot:

After 2 seconds:

댓글 수: 6
Gabriele
2021년 6월 6일
Scott MacKenzie
2021년 6월 6일
편집: Scott MacKenzie
2021년 6월 6일
Hmm, in that case, I'm not sure what to suggest.
BTW, I guess you have a reason for not setting the grid to the final size at the beginning and then just adding plots one by one until the grid is full
Gabriele
2021년 6월 6일
Scott's solution to use the flow TileArrangement is the closest you'll get to your goal using TiledLayout. It does everything you're asking for except for controlling the GridSize which is auto-updated every time you add a tile. But, as you've discovered, the tiledLayout must be set to flow before any tiles are added to the figure.
I agree, it would be nice to be able to redefine the grid. You could put in a feature request in the link below.
Since you can't set the position properties of tiled layout axes, you could create the axes with axes() and then write a function that updates their positions as needed. You can change the position properties of subplot too but if one subplot overlaps with another, it deletes the overlapped subplot so using axes is safer.
Gabriele
2021년 6월 7일
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

