Axes in dialog box turn blank after calling imagesc in pushbutton callback

조회 수: 3 (최근 30일)
Question: Why axes in dialog box go blank after calling imagesc from pushbutton callback? I call first imagesc after creation of axes and this works. But when axes are to be updated pushing the button, they go blank. I checked cData property of axes image object and it changed everytime after pushing the button.
MWE:
Dlg();
function Dlg()
% dialog window
d = dialog('Name', 'Random image', 'Position', [100 100 400 200]);
% axes
ax1 = axes('parent', d, 'Position', [0.45 0.1 0.5 0.8]);
imagesc(ax1, randi(10,10))
colormap(ax1, 'jet')
% pushbutton
uicontrol('Parent', d, 'Style', 'pushbutton', 'Position', [20, 130, 100, 50], ...
'String', 'Imagesc', 'Callback', @BtnPushed)
% pushbtn callback
function BtnPushed(~, ~)
imagesc(ax1, randi(10,10))
colormap(ax1, 'jet')
end
end
Thanks for explanation.

채택된 답변

Steve Eddins
Steve Eddins 2023년 3월 30일
편집: Steve Eddins 2023년 3월 30일
This is a strange one. The primary trigger for the unusual behavior is that the dialog function creates a figure with an empty colormap (0x3), unlike normal figures, which have a default colormap that is 256x3.
Then there are two other interactions that matter:
  • When you call colormap with a plain string or simple function handle, colormap(ax1,'jet') or colormap(ax1,@jet), the colormap function produces a new colormap that matches the length of the current colormap in ax1.
  • Inside imagesc, the call image(ax,cdata) is resetting the axes colormap. (I don't know if this is expected behavior. I'll ask the MATLAB Graphics team about that. Edit: see my comment below.) Since the parent figure has an empty colormap, the reset axes colormap also becomes empty.
Then, when the colormap function is called again by your callback function, the axes colormap has already been reset to 0x3, and so your colormap function is just producing another empty colormap.
I can think of two possible ways to change your implementation to avoid this behavior. First, you can change your calls to colormap to this: colormap(ax1, jet(256)).
Alternatively, you can revise your callback function to update the CData property of the existing image object, instead of calling imagesc to create a new up. Here's how that would look.
Dlg();
function Dlg()
% dialog window
d = dialog('Name', 'Random image','Position', [100 100 400 200]);
% axes
ax1 = axes('parent', d, 'Position', [0.45 0.1 0.5 0.8]);
im = imagesc(ax1, randi(10,10));
colormap(ax1, 'jet')
% pushbutton
uicontrol('Parent', d, 'Style', 'pushbutton', 'Position', [20, 130, 100, 50], ...
'String', 'Imagesc', 'Callback', @BtnPushed)
% pushbtn callback
function BtnPushed(~, ~)
im.CData = randi(10,10);
end
end
  댓글 수: 2
Steve Eddins
Steve Eddins 2023년 3월 30일
To follow up on the question I raised above, about imagesc(ax,A) resetting the axes colormap --
High-level graphics commands, such as imagesc, are expected to reset axes properties and delete existing axes children, unless you precede that call with something like hold on.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2023년 3월 30일
편집: Adam Danz 2023년 3월 30일
TL;DR Fix it by specifying the number of colors in jet()
function BtnPushed(~, ~)
imagesc(ax1, randi(10,10))
colormap(ax1, jet(256)) % <-----
end
What's happening
This stumped me for a bit. By default, figures produced by dialog have an empty colormap. When you specify a colormap using a string such as colormap(h,'jet'), it defines the number of colors in the colormap based on the figure's default colormap size. However, since dialog figures have an empty colormap, the colormap you're assigning is 0x3 which is no colormap at all. You can see that by calling,
colormap(ax1, 'jet')
size(ax1.Colormap)
> 0×3 empty double matrix
When you specify a colormap using an input argument, your colormap will have a non-zero height.
Another way this could have been fixed is by assigning a colormap to the dialog figure
d = dialog(__,'colormap',jet(256));
or by using a regular figure which has a non-empty default colormap and set to a modal WindowStyle to emulate dialog behavior.
d = figure('Name', 'Random image', 'Position', [100 100 400 200],'WindowStyle','modal')

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by