I am working on a GUI that is being initialized by creating several axes along with an invisible colorbar for each one (this is done so that axes maintain their predefined Position)1. The handles to all axes and colorbars are stored.
Interactions with the UI may result in images being plotted on any one axes. I would like only the colorbar of the active axes to be shown at any given time, by appropriately setting the Visible property of all colorbars.
I am having a problem with this approach, because I'm using imagesc to update my axes, and this deletes any colorbar associated with the axes, making the stored handles invalid.
My question is: how can I use imagesc or image to update an axes that is associated with a colorbar, without the colorbar being deleted?
Here's how to reproduce this problem:
dbclear in newplot %// Needed for the code to be properly re-runnable
%// Create an example figure containing a colorbar:
figure(); imagesc(imread('cameraman.tif')); colorbar;
%// "Refreshing" the displayed image:
uiwait(msgbox('The image will now be refreshed. A breakpoint will be set in newplot.m'));
dbstop in newplot at 124 %// The line responsible for deleting the colorbar in R2015A/B
imagesc(imread('cameraman.tif'));
The line on which the breakpoint in newplot.m is set reads:
cla(ax, 'reset', hsave);
Which is (un)surprisingly an undocumented way to invoke cla (with 3 parameters) that preserves objects whose handles are found in hsave.
Some ideas I had assuming colorbar deletion is inevitable (which I will pursue in case a "sane" solution is not found):
- Binding a
DeleteFcnto the colorbar that saves its data to somestruct. Creating a new colorbar afterimageschas finished, then iterating over the fields of thestructand assigning all properties to the new colorbar object. - Check "every once in a while" that all colorbars exist using either
findall(hFig,'type','colorbar')or verify that each of the axes has a validColorbarPeerHandleas per the Appendix below. Recreate the CB if invalid. - Delete all colorbars whenver different axes become active and create only the CB I'd like to show.
Appendix - ColorBar/Axes association:
Handle to the colorbar associated with a certain axes
hAxcan be obtained (in hg2) using:hCb = getappdata(hAx,'ColorbarPeerHandle');Handle to the
axesassociated with a colorbar objecthCbcan be obtained using2:hAx = hCb.Axes;