2017-02-08 7 views
0

Я пытаюсь сделать так, чтобы отображать в среднем четыре числа и функцию, которая принимает среднее значение в качестве своего ввода. Однако, если нажата кнопка, я хочу, чтобы функция отображала «0». Ниже приведена моя попытка, но проблема в том, что я пытаюсь сохранить состояние кнопки в handles.button_state, но мое значение похоже не хранится должным образом, поскольку глобальная переменная остается в моем инициализированном значении false, и проблема в том, что моя Если оператор выводит функцию или значение «0» всегда отображает значение функции, если я нажимаю кнопку «вычислять» более одного раза, вместо того, чтобы оставаться «0», если кнопка нажата.Matlab Guide Button Glitch-

function varargout = Real2(varargin) 

% REAL2 MATLAB code for Real2.fig 

%  REAL2, by itself, creates a new REAL2 or raises the existing 

%  singleton*. 

% 

%  H = REAL2 returns the handle to a new REAL2 or the handle to 

%  the existing singleton*. 

% 

%  REAL2('CALLBACK',hObject,eventData,handles,...) calls the local 

%  function named CALLBACK in REAL2.M with the given input arguments. 

% 

%  REAL2('Property','Value',...) creates a new REAL2 or raises the 

%  existing singleton*. Starting from the left, property value pairs are 

%  applied to the GUI before Real2_OpeningFcn gets called. An 

%  unrecognized property name or invalid value makes property application 

%  stop. All inputs are passed to Real2_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 Real2 




% Last Modified by GUIDE v2.5 07-Feb-2017 18:09:44 




% Begin initialization code - DO NOT EDIT 

gui_Singleton = 1; 

gui_State = struct('gui_Name',  mfilename, ... 

        'gui_Singleton', gui_Singleton, ... 

        'gui_OpeningFcn', @Real2_OpeningFcn, ... 

        'gui_OutputFcn', @Real2_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 Real2 is made visible. 

function Real2_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 Real2 (see VARARGIN) 




% Choose default command line output for Real2 

handles.output = hObject; 




% Update handles structure 

guidata(hObject, handles); 

initialize_gui(hObject, handles, false); 

% UIWAIT makes Real2 wait for user response (see UIRESUME) 

% uiwait(handles.figure1); 







% --- Outputs from this function are returned to the command line. 

function varargout = Real2_OutputFcn(hObject, eventdata, handles) 

% Get default command line output from handles structure 

varargout{1} = handles.output; 










%-----------------------------------------------------------------------------------% 













function Number1_Callback(hObject, eventdata, handles) 

Number1 = str2double(get(hObject, 'String')); 




handles.metricdata.Number1 = Number1; 

guidata(hObject,handles) 




% --- Executes during object creation, after setting all properties. 

function Number1_CreateFcn(hObject, eventdata, handles) 

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 

    set(hObject,'BackgroundColor','white'); 

end 










%-----------------------------------------------------------------------------------% 







function Number2_Callback(hObject, eventdata, handles) 

Number2 = str2double(get(hObject, 'String')); 

handles.metricdata.Number2 = Number2; 

guidata(hObject,handles) 




% --- Executes during object creation, after setting all properties. 

function Number2_CreateFcn(hObject, eventdata, handles) 

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 

    set(hObject,'BackgroundColor','white'); 

end 










%-----------------------------------------------------------------------------------% 













function Number3_Callback(hObject, eventdata, handles) 

Number3 = str2double(get(hObject, 'String')); 

handles.metricdata.Number3 = Number3; 

guidata(hObject,handles) 




% --- Executes during object creation, after setting all properties. 

function Number3_CreateFcn(hObject, eventdata, handles) 

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 

    set(hObject,'BackgroundColor','white'); 

end 



















function Number4_Callback(hObject, eventdata, handles) 

Number4 = str2double(get(hObject, 'String')); 

handles.metricdata.Number4 = Number4; 

guidata(hObject,handles) 

% --- Executes during object creation, after setting all properties. 

function Number4_CreateFcn(hObject, eventdata, handles) 

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 

    set(hObject,'BackgroundColor','white'); 

end 













%-----------------------------------------------------------------------------------% 

% -------------------------------------------------------------------- 

% --- Executes on button press in Togz. 

function Togz_Callback(hObject, eventdata, handles) 

% hObject handle to Togz (see GCBO) 

% eventdata reserved - to be defined in a future version of MATLAB 

% handles structure with handles and user data (see GUIDATA) 




% Hint: get(hObject,'Value') returns toggle state of Togz 

button_state = get(hObject,'Value'); 

handles.button_state = button_state; 

set(handles.funcz, 'String', 0); 

%-----------------------------------------------------------------------------------% 




% --- Executes on button press in pushbutton1. 

function pushbutton1_Callback(hObject, eventdata, handles) 

% hObject handle to pushbutton1 (see GCBO) 

% eventdata reserved - to be defined in a future version of MATLAB 

% handles structure with handles and user data (see GUIDATA) 




average = (handles.metricdata.Number1+handles.metricdata.Number2+handles.metricdata.Number3+handles.metricdata.Number4)/4; 

funcz= 2*average^2-3*average+2; 

set(handles.average, 'String', average); 




if handles.button_state==true 

    set(handles.funcz, 'String', 0); 

else 

    set(handles.funcz, 'String', funcz); 

end 










function initialize_gui(fig_handle, handles, isreset) 

% If the metricdata field is present and the reset flag is false, it means 

% we are we are just re-initializing a GUI by calling it from the cmd line 

% while it is up. So, bail out as we dont want to reset the data. 

if isfield(handles, 'metricdata') && ~isreset 

    return; 

end 




handles.metricdata.Number1 = 0; 

handles.metricdata.Number2 = 0; 

handles.metricdata.Number3 = 0; 

handles.metricdata.Number4 = 0; 

handles.button_state = false; 




set(handles.Number1, 'String', handles.metricdata.Number1); 

set(handles.Number2, 'String', handles.metricdata.Number2); 

set(handles.Number3, 'String', handles.metricdata.Number3); 

set(handles.Number4, 'String', handles.metricdata.Number4); 

set(handles.funcz, 'String', 1); 

set(handles.average, 'String', 0); 










% Update handles structure 

guidata(handles.figure1, handles); 

ответ

0

handles не является глобальной переменной, а скорее она хранится внутри самой фигуры и автоматически передаются на все обратные вызовы, как третий вход. Если вы хотите внести изменения в структуру handles, вы должны сохранить изменения в структуре handles, переустановив его на рисунок с помощью guidata.

Смежные вопросы