Handle does not store with assignment in GUI

1 回表示 (過去 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 Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by