Matlab gui - 如果直接运行.fig文件,则无法在按钮上设置文本框值

时间:2014-09-02 21:09:28

标签: matlab callback matlab-guide handles

我在matlab中为Bisection方法制作一个GUI,当我按下按钮计算根时,我在一个名为rezInput的文本框字段中显示结果。

如果我打开* .m文件并从这里运行gui - 一切正常。

问题:

如果我直接打开* .fig文件,请输入值(功能,间隔,容差)并按下按钮我收到错误'rezInput字段不存在'。

这是错误:

??? Reference to non-existent field 'rezInput'.
Error in ==> mBisectoarei>calcBtn_Callback at 212
set(handles.rezInput, 'String', num2str(xr));

Error in ==> gui_mainfcn at 96
    feval(varargin{:});

Error in ==> mBisectoarei at 42
gui_mainfcn(gui_State, varargin{:});

Error in ==>
@(hObject,eventdata)mBisectoarei('calcBtn_Callback',hObject,eventdata,guidata(hObject))


 ??? Error while evaluating uicontrol Callback

......以及整个代码:

     function varargout = mBisectoarei(varargin)
    % MBISECTOAREI M-file for mBisectoarei.fig

    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @mBisectoarei_OpeningFcn, ...
                       'gui_OutputFcn',  @mBisectoarei_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 mBisectoarei is made visible.
    function mBisectoarei_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 mBisectoarei (see VARARGIN)

    % Choose default command line output for mBisectoarei
    handles.output = hObject;

    % Update handles structure
    guidata(hObject, handles);

    % UIWAIT makes mBisectoarei wait for user response (see UIRESUME)
    % uiwait(handles.figure1);


    % --- Outputs from this function are returned to the command line.
    function varargout = mBisectoarei_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.output;

    function functieInput_Callback(hObject, eventdata, handles)
    % hObject    handle to functieInput (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,'String') returns contents of functieInput as text
    %        str2double(get(hObject,'String')) returns contents of functieInput as a double
    functie = get(hObject,'String');

    % save new value to global hObject for later use
    handles.tb.functie = functie;
    guidata(hObject,handles)

    % --- Executes during object creation, after setting all properties.
    function functieInput_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to functieInput (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called

    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end


    function intervalA_Callback(hObject, eventdata, handles)
    % hObject    handle to intervalA (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,'String') returns contents of intervalA as text
    %        str2double(get(hObject,'String')) returns contents of intervalA as a double
    intA = str2num(get(hObject,'String'));

    % save new value to global hObject for later use
    handles.tb.intA = intA;
    guidata(hObject,handles)

    % --- Executes during object creation, after setting all properties.
    function intervalA_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to intervalA (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called

    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end

    function intervalB_Callback(hObject, eventdata, handles)
    % hObject    handle to intervalB (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,'String') returns contents of intervalB as text
    %        str2double(get(hObject,'String')) returns contents of intervalB as a double
    intB = str2num(get(hObject,'String'));

    % save new value to global hObject for later use
    handles.tb.intB = intB;
    guidata(hObject,handles)

    % --- Executes during object creation, after setting all properties.
    function intervalB_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to intervalB (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called

    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end


    function tolInput_Callback(hObject, eventdata, handles)
    % hObject    handle to tolInput (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,'String') returns contents of tolInput as text
    %        str2double(get(hObject,'String')) returns contents of tolInput as a double
    tol = str2double(get(hObject,'String'));

    % save new value to global hObject for later use
    handles.tb.tol = tol;
    guidata(hObject,handles)

    % --- Executes during object creation, after setting all properties.
    function tolInput_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to tolInput (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called

    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end


    % --- Executes on button press in calcBtn.
    function calcBtn_Callback(hObject, eventdata, handles)
    % hObject    handle to calcBtn (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)

    % BEGIN mBisectoarei
    f = inline(handles.tb.functie);
    xl = handles.tb.intA;
    xu = handles.tb.intB;
    tol = handles.tb.tol;

    if f(xu)*f(xl)<0
    else
        set(handles.rezInput, 'String', 'Interval gresit!');
    end

    for i=2:1000
        xr=(xu+xl)/2;
        if f(xu)*f(xr)<0
            xl=xr;
        else
            xu=xr;
        end
        if f(xl)*f(xr)<0
            xu=xr;
        else
            xl=xr;
        end

        xnew(1)=0;
        xnew(i)=xr;

        if abs((xnew(i)-xnew(i-1))/xnew(i))<tol,break,end
    end
    set(handles.rezInput, 'String', num2str(xr));

    function rezInput_Callback(hObject, eventdata, handles)
    % hObject    handle to rezInput (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,'String') returns contents of rezInput as text
    %        str2double(get(hObject,'String')) returns contents of rezInput as a double


    % --- Executes during object creation, after setting all properties.
    function rezInput_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to rezInput (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called

    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end

1 个答案:

答案 0 :(得分:0)

不能通过直接打开.fig文件来运行MATLAB GUIS。看看this MathWorks链接。在标有&#34;保存GUI布局的部分下,&#34;找到第六(6)段。在屏幕截图下,您将看到&#34;注意:&#34;这解释了你的问题。