Axes tag disappears after imagesc

2 次查看(过去 30 天)
In my figure, created with 'guide', I have an axes object, which Tag is 'axes1'; After an 'imagesc' statement the Tag is empty. Why?
In the following example, I built programmatically the same figure and I haven't this problem:
function tagRemains()
% Create a figure window
fig = uifigure;
% Create a UI axes
ax = uiaxes('Parent',fig,...
'Units','pixels',...
'Tag','axes1',...
'Position', [104, 123, 300, 201]);
% Create a push button
btn = uibutton(fig,'push',...
'Position',[420, 218, 100, 22],...
'ButtonPushedFcn', @(btn,event) plotButtonPushed(btn,ax));
end
% Create the function for the ButtonPushedFcn callback
function plotButtonPushed(btn,ax)
x = linspace(0,2*pi,100);
y = sin(x);
plot(ax,x,y);
z = rand(100);
sprintf('Tag axes is %s', ax.Tag)
imagesc(ax,z);
sprintf('After imagesc tag axes is %s', ax.Tag)
end
Actually, my figure is more complex than the example, and in some operations I need to get the axis handle to do something, and the only way to get this handle is to find the graphic object from its tag. Can anyone explain me this behaviour? Did I something wrong?
Thank you

采纳的回答

Benjamin Kraus
Benjamin Kraus 2021-2-2
编辑:Benjamin Kraus 2021-2-2
The difference in behavior is due to the different value of the NextPlot property between axes and uiaxes.
Most graphics commands (including plot and imagesc) call a helper function called newplot. This command is designed to "prepare a figure and axes for subsequent graphics commands." It does this by checking the value of the NextPlot property on the axes:
  • If NextPlot is 'replace' the newplot command will reset the axes back to its default state (resetting all properties, including the Tag). This is the default value for regular axes (created with the axes command).
  • If NextPlot is 'replacechildren' the newplot command will delete the children of the axes, but won't reset any properties (so the Tag is preserved). This is the default value for uiaxes.
  • If NextPlot is 'add' the newplot command will not modify the axes or delete the children. When you turn hold on, the hold command sets the NextPlot property to 'add'.
In response to your comment about "in some operations I need to get the axis handle to do something".
My recommendation is to try to store the handle to your axes and reuse that, rather than relying on something like findobj (which I assume is how you are searching for the axes with your tag). You are already doing that in several places in your code. For example, you are already passing your axes handle into your callback function (plotButtonPushed) so you do not need to rely on the tag. This is the general approach I would recommend.
If that doesn't work, then you will either need to change the NextPlot value (either directly or using hold) before you call plot or imagesc, or reset the Tag after every call to a plotting function.
  2 个评论
Stephen23
Stephen23 2021-2-2
+1 an excellent explanation. This should be added to the FAQs.
Giuela
Giuela 2021-2-2
Thank you Benjamin, I'll follow your recommendation

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by