Handle does not store with assignment in GUI

조회 수: 7 (최근 30일)
Lawson Hoover
Lawson Hoover 2012년 12월 9일
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

채택된 답변

Jan
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.
  댓글 수: 1
Lawson Hoover
Lawson Hoover 2012년 12월 9일
Thank you! and I am still new to MATLAB and have been trying to figure this stuff out. But thank you again!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by