Error using tiledlayout syntax instead of subplot when referencing axes handles
조회 수: 21 (최근 30일)
이전 댓글 표시
Hello, I am trying to use the newer tiledlayout feature rather than subplots as I want to adjust the padding round each subplot. However, I have functions that require an axes handle (ax1, ax2).
Normally I do this: (and it works fine):
ax1=subplot(4,8,[1,2,3,9,10,11],'Parent',f1);
myImshow1(app,IM,n, ax1); %my own imshow function
ax2=subplot(4,8,[4,5,6,7,8,13,14,15,16],'Parent',f1);
myImshow1(app,IM2,n, ax2);
I've tried using tilelayout syntax:
t=tiledlayout(f1,2,2,'TileSpacing','Compact');
ax1=gca;
myImshow1(app,IM,n, ax1)
nexttile(t); ax2=gca;
myImshow1(app,IM2,n, ax2);
But it complains
댓글 수: 0
채택된 답변
Steven Lord
2021년 1월 21일
Be very careful about using gca, gcf, etc. in your code. I generally only use those when I'm experimenting in the Command Window.
Instead, call nexttile with an output argument. From its documentation page "ax = nexttile(___) returns the axes object." You can use the "Set Properties on the Axes" example on that documentation page as a model for your code, as it does something very similar to what you're trying to do.
댓글 수: 4
Steven Lord
2021년 1월 22일
Here's the example from the documentation page that I mentioned. I've added a few comments.
t = tiledlayout(2,1);
% First tile
ax1 = nexttile;
% You could specify ax1 as an input prior to [1 2 3 4 5] if you wanted
% to make it explicit that you were plotting into that tile.
plot([1 2 3 4 5],[11 6 10 4 18]);
ax1.XColor = [1 0 0];
ax1.YColor = 'k'; % Let's make/leave this black
% Second tile
ax2 = nexttile;
plot(ax2, [1 2 3 4 5],[5 1 12 9 2],'o'); % Here I added ax2 in the call
ax2.XColor = [0 0 1]; % Make this blue to distinguish it from ax1
ax2.YColor = [1 0 0];
Now if I wanted to, I could use ax1 or ax2 to manipulate those axes, add more stuff to those axes, etc.
text(ax1, 2.5, 12, ". This is (2.5, 12) in ax1")
If you're going to create a lot of these axes and keep their handles around, rather than using numbered variables you might want to store them in an array.
AX = gobjects(2, 1);
AX(1) = ax1;
AX(2) = ax2
추가 답변 (1개)
dpb
2021년 1월 21일
tiledplot is only able to put one axes object per tile, sorry. Another user wanted two y-axes just yesterday...
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!