Графический интерфейс Matlab - невозможно установить значение текстового поля при нажатии кнопки, если напрямую запустить файл .fig

Я делаю графический интерфейс для метода Bisection в Matlab, и когда я нажимаю кнопку для вычисления корня, я отображаю результат в текстовом поле с именем rezInput.

Если я открою *.m файл и отсюда запущу гуи - все работает.

Эта проблема:

Если я напрямую открываю файл *.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

person tbutcaru    schedule 02.09.2014    source источник


Ответы (1)


MATLAB GUIS нельзя запустить, напрямую открыв файлы .fig. Взгляните на это Ссылка MathWorks. В разделе «Сохранить макет графического интерфейса» найдите шестой абзац (6). Под скриншотом вы увидите «Примечание:», объясняющее вашу проблему.

person ServerS    schedule 02.09.2014
comment
Я ищу в Интернете эту ошибку уже почти два часа, и вы говорите мне, что так это должно работать. Хахаха! Это хорошо знать. Теперь я могу двигаться вперед. Спасибо!! - person tbutcaru; 03.09.2014