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

1 次查看(过去 30 天)
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 个评论
Douglas Anderson
Douglas Anderson 2014-4-30
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!

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by