2017-02-08 7 views
0

平均4つの数値とその平均値を入力とする関数を表示するようにしています。しかし、押しボタンが押された場合は、関数に "0"を表示させたい。私の試みは以下ですが、問題はボタンの状態をhandles.button_stateに保存しようとしていますが、グローバル変数が初期化された値falseのままであるため、私の値が正しく保存されていないように見えます。ボタンを押すと "0"のままではなく、 "calculate"ボタンを2回以上押すと、関数または値 "0"を表示するif文が関数の値を常に表示します。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を使用してFigureに再割り当てして、を保存する必要があります。

関連する問題