Problems with GUI deleting handles before output function

3 views (last 30 days)
I began making a GUI, called CTGH_2D_GUI, using Guide. This GUI will initialize at the beginning of another program so that the user can input data, which will be used in the rest of the program. However, I am having trouble getting output from the GUI. I am using the guidata() so that the handles can be used in another callback function. However, whenever I ran the program I always get the error "Attempt to reference field of non-structure array" from the output function. I typed "handles" into the line before the error, and it shows that the problem is the handles is empty. The way my GUI works is that there is push button called Run that assigns/changes the values to the handles, with guidata(hObject, handles) and close(handles.figure1) at the end. I was thinking that the guidata() wasn't working, but I played around with it to check. I removed the close function and then changed another pushbutton function so that it would display the handles when I pushed it. So, I hit the Run push button, and then pushed the 2nd button. All of the handles were displayed normally with the changes that had been made under the Run push button function. So, this tells me the handles are being passed on to other functions but not to the output function. I have also tried setappdata/getappdata instead and had the same error. My code is below. Does anyone know what may be causing it to delete the handles before the output function and/or a way to fix it?
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @CTGH_2D_GUI_OpeningFcn, ...
'gui_OutputFcn', @CTGH_2D_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 CTGH_2D_GUI is made visible.
function CTGH_2D_GUI_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 CTGH_2D_GUI (see VARARGIN)
% Choose default command line output for CTGH_2D_GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes CTGH_2D_GUI wait for user response (see UIRESUME)
uiwait(hObject);
% --- Outputs from this function are returned to the command line.
function varargout = CTGH_2D_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.Inlet_Gas_Temp;
delete(hObject);
% --- Executes on button press in Run.
function Run_Callback(hObject, eventdata, handles)
% hObject handle to Run (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
T_g_in=get(handles.Inlet_Gas_Temp,'String');
handles.Inlet_Gas_Temp=T_g_in;
guidata(hObject, handles);
close(handles.figure1);
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
% delete(hObject);
if isequal(get(hObject, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, call UIRESUME
uiresume(hObject);
else
% The GUI is no longer waiting, just close it
delete(hObject);
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 26 Jul 2015
Andrew - I created a simple GUI following your example (from above) and observed the same error message. I did notice that the figure1_CloseRequestFcn was never called though (I put a break point in this function). What I had to do was to go in to GUIDE and right-click on the figure1 to bring up the pop-up menu and choose View Callbacks and select CloseRequestFcn so that the GUI would be aware of this callback (it isn't enough just to copy and paste the code into the m file). Try making this "assignment" or "link" and re-run your code.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!