matlabuser-interfacematlab-figurematlab-guide

Error in using GUIDE in Matlab


This is the code for my GUI using GUIDE

function varargout = testcreat(varargin)
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @testcreat_OpeningFcn, ...
                       'gui_OutputFcn',  @testcreat_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

function testcreat_OpeningFcn(hObject, eventdata, handles, varargin)
    handles.output = hObject;
    guidata(hObject, handles);

function varargout = testcreat_OutputFcn(hObject, eventdata, handles) 
    varargout{1} = handles.output;

function edit1_Callback(hObject, eventdata, handles)

function edit1_CreateFcn(hObject, eventdata, handles)
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end

function pushbutton1_Callback(hObject, eventdata, handles)
    a=get(handles.edit1,'String');
    set(handles.text1,'String',a);
    guidata(hObject,handles); 

I am trying to build a GUI in Matlab and the above code is a test. However it doesn't run, and it gives error information like:

Attempt to reference field of non-structure array.
Error in testcreat>pushbutton1_Callback (line 104)
    a=get(handles.edit1,'String');
Error in gui_mainfcn (line 95)
        feval(varargin{:});
Error in testcreat (line 42)
    gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)testcreat('pushbutton1_Callback',hObject,eventdata,guidata(hObject))

Error while evaluating uicontrol Callback

Do anybody know why is it?


Solution

  • I finally figured it out. ORZ It is because i tried to use the GUI by directly operating in the .fig file. If so the data cannot transmit. And the correct way is to push the run button in the .m file!

    Hope nobody will do the same stupid thing like what i did.