Matlab 指南按钮故障-
Matlab Guide Button Glitch-
我试图让它显示四个数字的平均值和一个以平均值作为输入的函数。但是,如果按下按钮,我希望函数显示“0”。下面是我的尝试,但问题是我试图将按钮的状态存储在 handles.button_state
中,但我的值似乎没有正确存储,因为全局变量保持为我的初始化值 false
,问题是如果我多次按下 "calculate" 按钮,我显示函数或值“0”的 if 语句总是显示函数的值,而不是保持“0”,如果按下按钮。
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);
handles
不是全局变量,而是存储在图形本身中并作为第三个输入自动传递给所有回调。如果要对 handles
结构进行修改,则必须通过 re-assigning 将对 handles
结构的更改 保存 到图中使用 guidata
.
% Change the value
handles.button_state = button_state;
% "Save" these changes
guidata(hObject, handles)
我试图让它显示四个数字的平均值和一个以平均值作为输入的函数。但是,如果按下按钮,我希望函数显示“0”。下面是我的尝试,但问题是我试图将按钮的状态存储在 handles.button_state
中,但我的值似乎没有正确存储,因为全局变量保持为我的初始化值 false
,问题是如果我多次按下 "calculate" 按钮,我显示函数或值“0”的 if 语句总是显示函数的值,而不是保持“0”,如果按下按钮。
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);
handles
不是全局变量,而是存储在图形本身中并作为第三个输入自动传递给所有回调。如果要对 handles
结构进行修改,则必须通过 re-assigning 将对 handles
结构的更改 保存 到图中使用 guidata
.
% Change the value
handles.button_state = button_state;
% "Save" these changes
guidata(hObject, handles)