Handle does not store with assignment in GUI

1 visualización (últimos 30 días)
Lawson Hoover
Lawson Hoover el 9 de Dic. de 2012
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

Respuesta aceptada

Jan
Jan el 9 de Dic. de 2012
Editada: Jan el 9 de Dic. de 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 comentario
Lawson Hoover
Lawson Hoover el 9 de Dic. de 2012
Thank you! and I am still new to MATLAB and have been trying to figure this stuff out. But thank you again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by