MATLAB Answers

Axes tag disappears after imagesc

30 views (last 30 days)
Giuela
Giuela on 27 Jan 2021
Commented: Giuela on 2 Feb 2021
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

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 2 Feb 2021
Edited: Benjamin Kraus on 2 Feb 2021
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 Comments
Giuela
Giuela on 2 Feb 2021
Thank you Benjamin, I'll follow your recommendation

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!