I am trying to make it such that I display an average of four numbers and a function which takes the average as its input. However, if a pushbutton is pressed I want the function to display "0". Below is my attempt, but the problem is that I am attempting to store the state of the button in handles.button_state
but my value does not seem to store properly, as the global variable stays to my initialized value of false
, and the problem is that my if statement to either display the function or the value "0" always displays the value of the function if I press my "calculate" button more than once, instead of staying "0" if the button is pressed.
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
is not a global variable but rather it is stored within the figure itself and automatically passed to all callbacks as the third input. If you want to make a modification to the handles
structure, you have to save the changes to the handles
structure by re-assigning it to the figure using guidata
.
% Change the value
handles.button_state = button_state;
% "Save" these changes
guidata(hObject, handles)