"H must be the handle to a figure or figure descendent" when updating handles.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
This is a bit of a strange one I'm afraid. I have created a GUI which presents an image with a target which has a variable associated with it which relates to its 'contrast'. The image is presented in the GUI and the user clicks either 'present' or 'not present'. If the target is present and the user clicks 'not present', the contrast of the target is increased. The new contrast value is added to an array and guidata is used to update the handles object. However, after this occurs six times the program crashes with the error message:
??? Error using ==> guidata at 91
H must be the handle to a figure or figure descendent.
Error in ==> Colour_GUI>pushbutton3_Callback at 132
guidata(hObject, handles);
Error in ==> gui_mainfcn at 95
feval(varargin{:});
Error in ==> Colour_GUI at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)Colour_GUI('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
If the image user clicks ‘present’ when the target is present, the contrast value is reduced and the new constrast is added to the array as above. Confusingly, this does not cause the program to crash after a given number of cycles. The m-file associated with the GUI is below.
Any advice (or, equally, directions to useful reading material) would be much appreciated.
function varargout = Colour_GUI(varargin)
% COLOUR_GUI MATLAB code for Colour_GUI.fig
% COLOUR_GUI, by itself, creates a new COLOUR_GUI or raises the existing
% singleton*.
%
% H = COLOUR_GUI returns the handle to a new COLOUR_GUI or the handle to
% the existing singleton*.
%
% COLOUR_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in COLOUR_GUI.M with the given input arguments.
%
% COLOUR_GUI('Property','Value',...) creates a new COLOUR_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Colour_GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Colour_GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Colour_GUI
% Last Modified by GUIDE v2.5 18-Jun-2012 11:26:28
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Colour_GUI_OpeningFcn, ...
'gui_OutputFcn', @Colour_GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Colour_GUI is made visible.
function Colour_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.condition='colour';
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Colour_GUI (see VARARGIN)
handles.amplitudeList=[]';
handles.amplitude=0.01;
handles.present=1; %always present for to demonstrate error =
Stimulus=GenerateStimulus(handles.condition, handles.present, handles.amplitude);
imshow(Stimulus);
% Choose default command line output for Colour_GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Colour_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Colour_GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
if length(handles.amplitudeList)>5
max(abs(handles.amplitudeList(end-5:end-1)-handles.amplitudeList(end))/handles.amplitudeList(end))<0.05
end
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.present
handles.amplitude=handles.amplitude/1.01;
handles.amplitudeList=cat(1, handles.amplitudeList, handles.amplitude);
end
if length(handles.amplitudeList)>5 && max(abs(handles.amplitudeList(end-5:end-1)-handles.amplitudeList(end))/handles.amplitudeList(end))<0.05
CloseAndSave(handles)
end
handles.present=1; %always present for to demonstrate error =
Stimulus=GenerateStimulus(handles.condition, handles.present, handles.amplitude);
imshow(zeros(750, 950));
pause(.5);
imshow(Stimulus);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
if length(handles.amplitudeList)>5
max(abs(handles.amplitudeList(end-5:end-1)-handles.amplitudeList(end))/handles.amplitudeList(end))<0.05
end
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.present
handles.amplitude=handles.amplitude*1.01;
handles.amplitudeList=cat(1, handles.amplitudeList, handles.amplitude);
end
if length(handles.amplitudeList)>5 && max(abs(handles.amplitudeList(end-5:end-1)-handles.amplitudeList(end))/handles.amplitudeList(end))<0.05
CloseAndSave(handles)
end
handles.present=1; %always present for to demonstrate error =rand<0.5;
Stimulus=GenerateStimulus(handles.condition, handles.present, handles.amplitude);
imshow(zeros(750, 950));
pause(.5);
imshow(Stimulus);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in StopButton.
function StopButton_Callback(hObject, eventdata, handles)
% hObject handle to StopButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
CloseAndSave(handles)
function CloseAndSave(handles)
ampList=handles.amplitudeList;
save(['AmplitudeList_', handles.condition, '.mat'], 'ampList')
close all
0 comentarios
Respuesta aceptada
Tom
el 18 de Jun. de 2012
It appears to be because of this line: if length(handles.amplitudeList)>5 && max(abs(handles.amplitudeList(end-5:end-1)- handles.amplitudeList(end))/handles.amplitudeList(end))<0.05
You are sending to the close function- which uses close all- so you are closing the GUI and further on in the code you are trying to update it.
3 comentarios
Tom
el 18 de Jun. de 2012
I didn't realise that line cropped up a few times- in pushbutton 3 it can still send to the CloseAndSave function on the 6th iteration?
Más respuestas (2)
Sean de Wolski
el 18 de Jun. de 2012
The error message is telling you that you ar etryin got save the handles structure to an object which is not a figure and that this is not allowed. Why is this happening? Because hObject in the callback will be the object calling the callback, i.e. the uicontrol.
To store the handles structure at the end of a uicontrol callback, use
guidata(handles.figure1,handles);
Note fill in 'figure1' with the tag of your figure which is by default 'figure1'
Ver también
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!