Output from Gui results in error "Attempt to reference field of non-structure array"

Hello,
I have written a very simple GUIDE Gui to return the value of a slider to the command line. I set up a default value, and have a "Done" button. If the uiwait{} is commented out, the default values are returned to the command line (when requested), and if I decomment the uiwait(), I get the error message.
Again, the GUIDE figure consists of the slider with a text value that documents where the slider is, and a DONE button. That's it.
I have tried all kinds of permutations of the code, and this behavior mystifies me. Any thoughts as to why this happens? Thanks!
Doug Anderson
The code follows:
function varargout = foobar1(varargin)
% FOOBAR1 MATLAB code for foobar1.fig
% FOOBAR1, by itself, creates a new FOOBAR1 or raises the existing
% singleton*.
%
% H = FOOBAR1 returns the handle to a new FOOBAR1 or the handle to
% the existing singleton*.
%
% FOOBAR1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FOOBAR1.M with the given input arguments.
%
% FOOBAR1('Property','Value',...) creates a new FOOBAR1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before foobar1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to foobar1_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 foobar1
% Last Modified by GUIDE v2.5 30-Apr-2014 10:06:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @foobar1_OpeningFcn, ...
'gui_OutputFcn', @foobar1_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 foobar1 is made visible.
function foobar1_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 foobar1 (see VARARGIN)
%Set Slider Starting Values
max_slider = 100;
min_slider = 1;
default_slider = 50;
slider_step = 1 / (max_slider - min_slider);
jumper = 1;
set(handles.slider_value,'max',max_slider);
set(handles.slider_value,'min',min_slider);
set(handles.slider_value,'value',default_slider);
set(handles.slider_value,'sliderstep',[slider_step,jumper]);
set(handles.slider_text,'string',default_slider);
handles.exit = default_slider;
% Choose default command line output for foobar1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes foobar1 wait for user response (see UIRESUME)
%uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = foobar1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to b e 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;
varargout{2} = handles.exit;
%delete(handles.figure1);
% --- Executes on button press in done_button.
function done_button_Callback(hObject, eventdata, handles)
% hObject handle to done_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
uiresume(handles.figure1);
delete(handles.figure1);
% --- Executes on slider movement.
function slider_value_Callback(hObject, eventdata, handles)
% hObject handle to slider_value (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
set(handles.slider_text,'string',round(get(handles.slider_value,'value')));
handles.exit = round(get(handles.slider_value,'value'));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function slider_value_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider_value (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes when user attempts to close 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
% Check appdata flag to see if the main GUI is in a wait state
%{
if getappdata(handles.figure1,'waiting')
% The GUI is still in UIWAIT, so call UIRESUME and return
uiresume(hObject);
setappdata(handles.figure1,'waiting',0)
else
% The GUI is no longer waiting, so destroy it now.
delete(hObject);
end
%}
delete(hObject);

9 Comments

Hi Douglas
It's so clever that you put the whole code, it would be even better if you please put the whole error message and it's corresponding line, every single info about the error could be helpful for unpaintient users like me to take a look at your code.
Regards ;)
Thank you for asking! Here is the MATLAB response:
>> foobar1
Attempt to reference field of non-structure array.
Error in foobar1>foobar1_OutputFcn (line 89)
varargout{1} = handles.output;
Error in gui_mainfcn (line 265)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in foobar1 (line 42)
gui_mainfcn(gui_State, varargin{:});
By the way, as I mentioned, I can try to get output ( [x y] = foobar; ) with the uiwait commented, and it returns the handle in x and the default slider value of 50 in y. If I uncomment the uiwait, I get this error. So I can't get the action of the slider to give me the "y". I really don't need the handle to the figure.
Hi Douglas - I followed the instructions for something similar at the video http://blogs.mathworks.com/videos/2010/02/12/advanced-getting-an-output-from-a-guide-gui/ and found that I had to associate the CloseRequestFcn with the figure via the properties tab (or as shown in the video, right-clicking on the figure and selecting View Callbacks -> CloseRequestFcn). I then added the code
if isequal(get(hObject, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, us UIRESUME
uiresume(hObject);
else
% The GUI is no longer waiting, just close it
delete(hObject);
end
as the CloseRequestFcn body, uncommented the uiwait, and modified the OutputFcn body as:
varargout{1} = get(handles.slider_value,'Value');
I launched the GUI (from the command line), moved the slider, closed the GUI, and the slider value was printed to the command line.
Note that if I didn't do the association of the CloseRequestFcn with the figure, then if I ran the code, I would get the same error as you (since the handles input to the OutputFcn was empty (and so, no field to reference).
Geoff
Geoff
Thanks again. This works, except that I can't figure out how to close this with the "Done" button (don't want to have to close the figure with the "x" closing thing). Any suggestions?
Perhaps try close(handles.figure1); within the body of the done button callback.
Yes, Geoff, that worked.
It still left the figure open, so I put
delete(handles.figure1)
at the end of the OutputFcn.
Boy, some of this is so arcane and mysterious!
Thanks!
Doug
OH!!!!!!!!!!!!!!!!!!
I see where the problem in Doug Hull's Video is!!!!!!!
You need to add a guidata(hObject,handles) in the CloseRequestFcn, like follows:
if isequal(get(hObject,'waitstatus'),'waiting')
%if getappdata(handles.figure1,'waiting')
% The GUI is still in UIWAIT, so call UIRESUME and return
uiresume(hObject);
guidata(hObject,handles);
else
% The GUI is no longer waiting, so destroy it now.
delete(hObject);
end
That way you can actually use the handles.exit and not have to call the get() in the OutputFcn. That's actually what I wanted, since the real code (not the foobar) has a structure containing the info from a bunch of sliders, all of which will be put into a handles.exit structure.
Doug Hull uses the getappdata/setappdata, but I just wanted to use the handles structure.
But thanks again, you really helped!

Sign in to comment.

Answers (0)

Products

Asked:

on 30 Apr 2014

Commented:

on 30 Apr 2014

Community Treasure Hunt

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

Start Hunting!