Handle does not store with assignment in GUI

1 view (last 30 days)
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

Accepted Answer

Jan
Jan on 9 Dec 2012
Edited: Jan on 9 Dec 2012
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 Comment
Lawson Hoover
Lawson Hoover on 9 Dec 2012
Thank you! and I am still new to MATLAB and have been trying to figure this stuff out. But thank you again!

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!