Handle does not store with assignment in GUI
1 次查看(过去 30 天)
显示 更早的评论
I am trying to change the background image based off of selection from a context menu. When I run it it says that S.Ih is an Invalid Handle.
Code:
ImAxes = axes('Parent',S.fh,'units','pixels','Position',[0 0 800 550],'HitTest','off');
S.Ih = imread('storm.jpg');
%S.Ih = image(X);
Im = imagesc(S.Ih,'HitTest','off');
set(ImAxes,...
'handlevisibility','off', ...
'visible','off');
S.UCM = uicontextmenu;
bgmenu = uimenu(S.UCM,'label','Background');
S.um(1) = uimenu(bgmenu,'Label','Grid');
S.um(2) = uimenu(bgmenu,'Label','ConTrail');
S.um(3) = uimenu(bgmenu,'Label','Opera');
S.um(4) = uimenu(bgmenu,'Label','Light Wood');
set(S.fh,'uicontextmenu',S.UCM)
set(S.um,'CallBack',@menu_call_bg);
guidata(S.fh,S) % Save the structure for later use.
function [] = menu_call_bg(varargin)
S = guidata(gcbf);
switch gcbo
case S.um(1)
I = imread('grid.jpg');
case S.um(2)
I = imread('ConTrail.png');
case S.um(3)
I = imread('Opera.jpg');
case S.um(4)
I = imread('light wood.jpg');
otherwise
end
set(S.Ih,'cdata',I)
end
0 个评论
采纳的回答
Jan
2012-12-9
编辑:Jan
2012-12-9
The command imread does not reply a handle, but the contents of the image file, see doc imread. The following line has been commented, although it seems to solve the problem already:
X = imread('storm.jpg');
S.Ih = image(X);
The next line looks confusing also:
Im = imagesc(S.Ih,'HitTest','off');
Please read the documentation of the imagesc command again. Now S.Ih is treated as image data again, but later on in set(S.Ih,'cdata',I) you want to access this as handle.
It is much more efficient to use the debugger than to ask the forum for such problems. Type this in the command line:
dbstop if error
and start the program again. Then Matlab stops, when the problem occurs and you can check the values of the variables.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!