My program quits when I run a callback function in GUI.
1 view (last 30 days)
Show older comments
Inside the function name_OpeningFcn(hObject, eventdata, handles, varargin) I call one of the callbacks functions. After it runs, instead of continuing the code in name_OpeningFcn, it jumps directly to the end of vargaout, just before "% END initialization code - DO NOT EDIT".
function varargout = name(varargin)
% ...........
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @graphicsdachshund2_OpeningFcn, ...
'gui_OutputFcn', @graphicsdachshund2_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
end
% --- Executes just before name is made visible.
function name_OpeningFcn(hObject, eventdata, handles, varargin)
% 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 name (see VARARGIN)
if uicontrol('Style','pushbutton','Callback',@pushbutton2_Callback)==1
pushbutton2_Callback(hObject, eventdata, handles);
disp('This is where it will not reach');
end
handles.output = hObject;
guidata(hObject, handles);
end
% --- Outputs from this function are returned to the command line.
function varargout = name_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
end
% --- Executes on button press in pushbutton2 = Granger struct
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file path] = uigetfile('*.mat');
if file == 0
%disp('No file selected')
handles.error = 'You did not upload any file';
else
handles.filepath = [file path];
end
guidata(hObject, handles);
disp('This is the last thing it displays');
end
0 Comments
Answers (1)
Jan
on 13 Jan 2019
This line:
if uicontrol('Style','pushbutton','Callback',@pushbutton2_Callback)==1
will not enter the code in the if block. uicontrol creates a new pushbutton and replies the handle to it. This handle will never be 1: In modern Matlab versions handles are no numbers anymore, and in old Matlab versions, only figures got integer numbers as handles. This means, the condition is false in every case and this line is not reached also:
pushbutton2_Callback(hObject, eventdata, handles);
I cannot guess, what the "if uicontrol(..." line should do. I assume, the code works, if you omit this line and the corresponding end. I do not understand, why the code does reach
disp('This is the last thing it displays');
I recommend to use the debugger to step through the code line by line. Set a breakpoint in the first line and use the "Step " and "Step in" buttons to evaluate the code step by step. Then it gets clear immediately, where it branches to which line.
2 Comments
Image Analyst
on 13 Jan 2019
And I never figured out WHY, if he's using GUIDE, is he trying to create a new pushbutton in the OpeningFcn() of the GUI instead of just creating the other pushbutton in GUIDE to begin with!?!?!?
See Also
Categories
Find more on Event Functions 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!