How to save (or prevent from being deleted) handle to axes within a GUI (created using GUIDE) ?
38 views (last 30 days)
Show older comments
Sowmya Aggarwal
on 24 Apr 2017
Answered: Veronica Taurino
on 11 May 2022
My GUI contains a single axes space. Handle to that axes on GUI initialization is handles.axes1. I have enabled multiple callback buttons on the GUI to plot 1) scatter, 2) line plot, 3) bar graph. I set axes to handles.axes1 using axes(handles.axes1). Upon initialization, isvalid(handles.axes1) = 1, and all plots turn out within this axes space, however, after one round of plotting 1 - 3 plots above, when I re-click on any, Matlab returns error message displaying, handles to axes not found, i.e., handles.axes1 points to a deleted object. I am not deleting handles.axes1 during any of the callbacks. How to prevent this deletion? How to effectively save/re-initialize handles.axes1 without having to reinitialize GUI? Even if I save axes handles into another object, the object gets deleted eventually. Because of this I have to manually set axes position and on multi-platform this is not resulting in favorable results, the plots get partially hidden (or shifted to right/left/top/bottom of axes space) depending on screen resolution, version of matlab etc. Favorable outcome will be, I get to save (or prevent from deletion) handles.axes1 and keep using it to set axes between multiple callbacks for plotting.
0 Comments
Accepted Answer
Image Analyst
on 24 Apr 2017
Search your code for "clear" - chances are you're calling clear somewhere in a callback and this blows away the handle structure inside any function where you called clear.
More Answers (2)
Benjamin Kraus
on 5 Jun 2017
Your best bet to avoid the axes being deleted is to always pass an axes handle when you are calling your plotting functions.
Functions like scatter call newplot. Depending on the value of the NextPlot property on the axes and the figure, newplot may delete your axes. However, if you specify an axes handle when you call plotting functions (like scatter), that axes handle is passed on to newplot, and newplot will make sure to avoid deleting that axes (if possible).
Here's an example:
close all
ax = axes;
f = ax.Parent;
plotmatrix(magic(5))
% Because plotmatrix creates a lot of axes within the figure,
% it sets the NextPlot property on the figure to 'replacechildren',
% so the extra axes are deleted the next time you try to plot.
f.NextPlot % Return value will be 'replacechildren'
ax % The axes you created initially still exists after the call to plotmatrix.
scatter(rand(1,10),rand(1,10))
ax % Scatter called newplot, which deleted the axes.
If you pass in axes handles, the code works a little differently:
close all
ax = axes;
f = ax.Parent;
plotmatrix(ax, magic(5))
% Because plotmatrix creates a lot of axes within the figure,
% it sets the NextPlot property on the figure to 'replacechildren',
% so the extra axes are deleted the next time you try to plot.
f.NextPlot % Return value will be 'replacechildren'
ax % The axes you created initially still exists after the call to plotmatrix.
scatter(ax, rand(1,10),rand(1,10))
ax % Scatter called newplot, but because you provided an axes handle, the axes is saved.
The moral of the story is to always use axes handles when making a UI with GUIDE.
scatterhist behaves a little differently than plotmatrix because it expects to be plotting into a figure or panel, not a particular axes:
close all
ax = axes;
f = ax.Parent;
scatterhist(randn(250,1),randn(250,1))
ax % The axes you created initially has been deleted and replaced.
Unfortunately, scatterhist deletes all the children of the figure or panel it is plotted into. Your best bet is to create a panel just for the scatterhist and pass that as a parent. scatterhist does not follow the convention of allowing you to specify the parent as the first input argument, but you can specify a parent using a name/value pair.
close all
ax = axes;
f = ax.Parent;
p = uipanel('Parent',f);
scatterhist(randn(250,1),randn(250,1),'Parent',p)
ax % The axes you created initially still exists, but is covered by the panel.
When you are done with the panel, you can set its Visible property to 'off' until the next time you need to create a scatterhist.
Veronica Taurino
on 11 May 2022
Maybe I can contribute, I was in a similar situation.
I had to add a plot3 over several axes, but the handles were deleted during a for-loop.
At first, I solved it calling each axis in the for-loop, but it slowed a lot the workflow:
for ii=1:NumAxis
ax = AxesStored(ii);
% do stuff..
end
I solved it just adding the following line before the for-loop:
[AxesStored.NextPlot]=deal('add');
for ii=1:NumAxis
% do stuff..
end
0 Comments
See Also
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!