Axes tag disappears after imagesc

조회 수: 2 (최근 30일)
Giuela
Giuela 2021년 1월 27일
댓글: Giuela 2021년 2월 2일
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