Calling linkaxes on uiaxes objects makes plot contents disappear when using uigridlayout

9 次查看(过去 30 天)
I wish to link the windows of a set of uiaxes. For regular axes objects, I would call the linkaxes function to link their windows together. In the following example, calling the linkaxes function for uiaxes objects makes any plots on these axes disappear:
% Generate UIFigure
ufig = uifigure;
% Apply grid layout to UIFigure
gl = uigridlayout(ufig, 'RowHeight', {'1x', '1x', '1x'}, 'ColumnWidth', {'1x'});
% Create three uiaxes objects; place them in the grid layout
ax1 = uiaxes(gl);
ax1.Layout.Row = 1;
ax2 = uiaxes(gl);
ax2.Layout.Row = 2;
ax3 = uiaxes(gl);
ax3.Layout.Row = 3;
% Create plots for each uiaxes object
x = 1:10;
plot(ax1, x, x);
plot(ax2, x, x.^2);
plot(ax3, x, x.^3);
% Attempt to link the uiaxes together
linkaxes([ax1 ax2 ax3]);
It is the last line of this code (with linkaxes) that makes the plots disappear. Otherwise, they show up as expected.
I believe the use of uigridlayout is contributing to the problem. In the following code snippet, which does not include the use of uigridlayout, linking the uiaxes objects together with linkaxes does not make the plots disappear:
% Generate UIFigure
ufig = uifigure;
% Create three uiaxes objects; place them side by side
ax1 = uiaxes(ufig, 'Position', [ 0 0 200 200]);
ax2 = uiaxes(ufig, 'Position', [200 0 200 200]);
ax3 = uiaxes(ufig, 'Position', [400 0 200 200]);
% Create plots for each uiaxes object
x = 1:10;
plot(ax1, x, x);
plot(ax2, x, x.^2);
plot(ax3, x, x.^3);
% Attempt to link the uiaxes together
linkaxes([ax1, ax2, ax3]);
I would appreciate any assistance that one could offer to help me understand and, if possible, correct this issue.
  2 个评论
Adam Danz
Adam Danz 2024-6-6
编辑:Adam Danz 2024-6-6
I was able to reproduce the problem in R2023b but not in R2024a. It appears that linkaxes is executed before the line objects affect the axes limits.
When linkaxes is called, the axes limits (xlim, ylim) are set to manual which means they won't update. This appears to happen too early and the axes limits are set at their default values [0,1] rather than first expanding to the extent of the line.
If you run the code except for the linkaxes line and then run the linkaxes line afterwards, everything is fine because the axes limits have been correctly updated.
The easiest workaround is to put a drawnow before linkaxes so the line is rendered and the axes limits are adjusted before linkaxes takes affect, as explained in the answer below.
Ted Londner
Ted Londner 2024-6-6
编辑:Ted Londner 2024-6-6
Hi @Adam Danz. Thank you for your comment. Could you please clarify what "solutions" you are referring to? Are you referring to @Matlab Pro's answer?
Regardless, I will try upgrading to R2024a and will see if the problem goes away.
[responding to your subsequent edit...]
Thank you for the information!

请先登录,再进行评论。

采纳的回答

Matlab Pro
Matlab Pro 2024-6-6
This is really wierd! well, I tried your code.
It looks like some rendering problem..
Anyhow - add the next 2 lines to the code, just before the linkaxes - and.. you'll see tha magic happening ...
ufig.focus();
drawnow();
  3 个评论
Ted Londner
Ted Londner 2024-6-6
Hi, @Matlab Pro. Thank you for your answer. Your suggestion worked for my first code snippet. That snippet is a pared down version of an app that I am building. Strangely, your fix did not resolve the problem in that app.
Building upon your suggestion, I tried including a brief pause before the focus() command, and interestingly, that made the plots show up as expected.
To summarize—
This did not work in my app:
app.UIFigure.focus();
drawnow();
This did work in my app:
pause(0.25);
app.UIFigure.focus();
drawnow();
Smaller pause durations, such as 0.1, did not work, strangely.
Any idea what's happening in MATLAB to cause the issue?
Matlab Pro
Matlab Pro 2024-6-6
I expected that drawnow() will be the answer, but (in my testing for the exact problem above) - did not suffice... just after adding the focus() - the plot was updated..

请先登录,再进行评论。

更多回答(1 个)

Zinea
Zinea 2024-6-7
The issue of the plots not visible is due to the scaling of the axes. The figure on executing the code snippet 1 (as given by you) is as follows:
The plots are not visible here. But if you zoom out, the plots are visible, and you get the following:
NOTE: This issue arises in MATLAB R2023b as the y axis has default tick labels at 0.1, 0.2, ..., and so on. But in MATLAB R2024a, the axis ticks are auto scaled and the plot is visible, without the need to zoom out.
Hope it helps!
  1 个评论
Ted Londner
Ted Londner 2024-6-7
This is helpful, @Zinea, thank you.
As you and @Adam Danz noted, this problem is not reproducable in R2024a. After upgrading to R2024a last night, I no longer observe the problem. Thank you again to you both.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by