MATLAB: H must be the handle to a figure or figure descendent

handles

function varargout = PaintGUI(varargin)
% PAINTGUI MATLAB code for PaintGUI.fig
% PAINTGUI, by itself, creates a new PAINTGUI or raises the existing
% singleton*.
%




% H = PAINTGUI returns the handle to a new PAINTGUI or the handle to
% the existing singleton*.
%
% PAINTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PAINTGUI.M with the given input arguments.
%
% PAINTGUI('Property','Value',...) creates a new PAINTGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before PaintGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to PaintGUI_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 PaintGUI
% Last Modified by GUIDE v2.5 26-Mar-2019 23:36:37
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @PaintGUI_OpeningFcn, ...
'gui_OutputFcn', @PaintGUI_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 PaintGUI is made visible.
function PaintGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for PaintGUI
handles.output = hObject;
h = findobj('Tag','Gui1');
handles.Gui1data = guidata(h);
axes(handles.axes1);
handles.Image=handles.Gui1data.OriginalImage;
imshow(handles.Image);
% Update handles structure
guidata(handles.axes1, handles);
% UIWAIT makes PaintGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = PaintGUI_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)
c=uisetcolor;
cc=[255,255,255];
cc=cc.*c;
handles.Color=cc;
handles.text10.BackgroundColor=c;
guidata(hObject.handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
handles.Result = Paint(handles.Image, handles.Color);
axes(handles.axes1);
imshow(handles.Result);
handles.Image=handles.Result;
guidata(hobject, handles);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
setappdata(0, 'ReturnImage', handles.Result);
% --- Executes on button press in File.
function File_Callback(hObject, eventdata, handles)
[a b]=uigetfile ({'*.jpg', 'All files'});
handles.Image = imread ([b a]);
imshow(handles.Image, 'Parent', handles.axes1);
Can anyone help me to check what is the problem in my coding? I am currently doing a project titled 'Image Coloring'.
The following errors appear when I trying to run the code.
Error using guidata (line 87)
H must be the handle to a figure or figure descendent.
Error in PaintGUI>PaintGUI_OpeningFcn (line 59)
handles.Gui1data = guidata(h);
Error in gui_mainfcn (line 220)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in PaintGUI (line 42)
gui_mainfcn(gui_State, varargin{:});

Best Answer

Use the debugger to analyse the problem. Type e.g. in the command window:
dbstop if error
Run the code again and when Matlab stops at the error, check the contents of the variable:
h
class(h)
size(h)
Maybe you have multiple objects with the tag 'GUI1'. The code
h = findobj('Tag','Gui1');
should be accompanied by a check:
if numel(h) ~= 1
error('Cannot find the object ''Gui1'' uniquely.');
end
A hint: Prefer fullfile instead of [b a] and add a check, if the user pressed Cancel:
[filename, pathname] = uigetfile ({'*.jpg', 'All files'});
if ~ischar(filename)
fprintf(2, 'User canceled the file choosing.\n');
return;
end
handles.Image = imread(fullfile(pathname, filename));
Related Question