Display a new image inside existing axes without removing the color bar

I am working on a graphical interface that is initialized by creating several axesalong with the invisible colorbarfor each of them (this is done so that the axes retain their predefined Position) 1 . Handles for all axes and color panels are retained.

Interaction with the user interface can lead to the fact that images will be displayed on any axis. I would like to see only the color bar of the active axes at any given time, setting the property Visiblefor all color panels accordingly .

I have a problem with this approach because I use imagescto update my axes and this removes any color-related panel with axes, making the stored descriptors invalid.

My question is: how can I use imagescor imageto update the axes associated with the color panel without removing the color panel?

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 is set at newplot.mreads:

cla(ax, 'reset', hsave);

Which is (un) a surprisingly undocumented way to invoke cla(with 3 parameters) that saves the objects whose descriptors are found in hsave.


Some ideas that I took when removing the colorbar are inevitable (what will I pursue if a β€œreasonable” solution is not found):

  • a DeleteFcn , struct. imagesc , struct colorbar.
  • " ", findall(hFig,'type','colorbar') , ColorbarPeerHandle . CB, .
  • , , CB, .

- ColorBar/Axes:

  • , hAx, ( hg2), :

    hCb = getappdata(hAx,'ColorbarPeerHandle');
    
  • axes, colorbar hCb, 2:

    hAx = hCb.Axes;
    
+4
1

:

  • safeUpdateImage1 - cla(...) Image hold. , Image .
  • safeUpdateImage2 - mikkola, Image. , ColorBar an Image, .

:

function [] = Problem
dbclear in newplot %// Needed for the code to be properly re-runnable
%// Create an example figure containing a colorbar:
Img = imread('cameraman.tif');
figure(); imagesc(Img); hAx = gca; colorbar;
%// Refreshing the displayed image (comment/uncomment as needed):
switch questdlg('Please select an image update method:','Update method selection',...
                'Broken','Safe1','Safe2','Safe1')
  case 'Broken'
    brokenUpdateImage(hAx,255-Img);
  case 'Safe1'
    safeUpdateImage1(hAx,255-Img);
  case 'Safe2'
  safeUpdateImage2(hAx,255-Img);  
end
end

function brokenUpdateImage(hAx,newImg)
uiwait(msgbox('The image will now be refreshed. A breakpoint will be set in NEWPLOT'));
dbstop in newplot at 124 %// The line responsible for deleting the colorbar in R2015A/B
imagesc(newImg,'Parent',hAx);
end

% For cases when the only desired child is an Image and the axes contents are unknown
function safeUpdateImage1(hAx,newImg,imgType,imgUpdateFcn)
if nargin < 4 || isempty(imgUpdateFcn)
  imgUpdateFcn = @imagesc;
end
if nargin < 3 || isempty(imgType)
  imgType = 'Image';  
end
if strcmp(hAx.NextPlot,'replace') %// Equivalent to checking "ishold == false"
  hAx.NextPlot = 'add'; %// Equivalent to "hold on"
  %// hCurrImgs = get(hAx,'Children'); %// Deletes all types of Children
  hCurrImgs = findall(hAx,'type','Image'); %// Deletes only graphical objects of type 
                                           %// "matlab.graphics.primitive.Image"
  for ind1=1:numel(hCurrImgs)
    delete(hCurrImgs(ind1));
  end
  imgUpdateFcn(newImg,'Parent',hAx);
  if strcmpi(imgType,'Image')
    axis(hAx,'tight','ij');    
%// 'tight' - XLimMode, YLimMode, and ZLimMode change to 'auto'. The limits automatic- 
%//           ally update to incorporate new data added to the axes. To keep the limits
%//           from changing when using hold on, use axis tight manual.
%// 'ij'  β€”   Reverse direction. For axes with a 2-D view, the y-axis is vertical with
%//           values increasing from top to bottom.    
  end
end
end

%// When it known that the axes contains at least one Image:
function safeUpdateImage2(hAx,newImg,~,~)
%// <Input checking code>
hCurrImgs = findall(hAx,'type','Image');
%// <Optional code to decide which Image child to update>
hCurrImgs(1).CData = newImg; %// In this example, only update the "topmost" child
end
+2

Source: https://habr.com/ru/post/1617354/


All Articles