:
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