My program quits when I run a callback function in GUI.

1 次查看(过去 30 天)
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

回答(1 个)

Jan
Jan 2019-1-13
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 个评论
Image Analyst
Image Analyst 2019-1-13
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!?!?!?
Ana Risnoveanu
Ana Risnoveanu 2019-1-13
编辑:Ana Risnoveanu 2019-1-13
What I hoped the line would do is check if the button was pressed or not, so that I can call the function pushbutton2_Callback.
What I really want to do is use the push button to upload a filepath and then load it in the name_OpeningFcn function. So, if I write :
function pushbutton2_Callback(hObject, eventdata, handles)
[file path] = uigetfile('*.mat');
if file == 0
handles.error = 'You did not upload any file';
else
handles.filepath = [file path];
end
guidata(hObject, handles);
end
And then inside the name_OpeningFcn I try to load it:
data = load(handles.filepath);
This is the error:
Reference to non-existent field 'filepath'.
Error in graphicsdachshund2>graphicsdachshund2_OpeningFcn (line 67)
data = load(handles.filepath);
Error in gui_mainfcn (line 220)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in graphicsdachshund2 (line 42)
gui_mainfcn(gui_State, varargin{:});

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Event Functions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by