I have a simple GUI with 2 pushbuttons, one says yes the other says no.
How can I record how many times each has been pressed and more importantly the order?
Thanks.
To count the number of "Yes" and "No" you might define in your GUI:
To keep track of the sequence of the "Yes" and "No" you might defined
All the above defined variable can be initialized in the GUI OpeningFcn
and then stored in the structure of the GUI data
(by using the guidata
finction.
The variable are then:
pushbutton callback
GUI data
(again with the guidata
functionIn the "Yes" and "No" pushbutton callback
you can also print either the number of times the "Yes" and "No" pushbutton have been pressed and their sequence in two text box
.
You can also use the array in which you've stored the "Yes" / "No" as 1
and -1
to, for example, plot a graph.
In the following, the coded of the GUI yes_no_counter
used to test the solution; in the GUI:
pushbutton tag
= `yes_btnpushbutton tag
= no_btn
text box tag
= "text1"text box tag
= "text2"yes_no_counter.m
function varargout = yes_no_counter(varargin)
% YES_NO_COUNTER MATLAB code for yes_no_counter.fig
% YES_NO_COUNTER, by itself, creates a new YES_NO_COUNTER or raises the existing
% singleton*.
%
% H = YES_NO_COUNTER returns the handle to a new YES_NO_COUNTER or the handle to
% the existing singleton*.
%
% YES_NO_COUNTER('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in YES_NO_COUNTER.M with the given input arguments.
%
% YES_NO_COUNTER('Property','Value',...) creates a new YES_NO_COUNTER or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before yes_no_counter_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to yes_no_counter_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 yes_no_counter
% Last Modified by GUIDE v2.5 09-Jan-2016 17:22:23
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @yes_no_counter_OpeningFcn, ...
'gui_OutputFcn', @yes_no_counter_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 yes_no_counter is made visible.
function yes_no_counter_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 yes_no_counter (see VARARGIN)
% Choose default command line output for yes_no_counter
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes yes_no_counter wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% Get the GUI data
my_gui_data=guidata(gcf);
% Initialize the "Yes / No " counter
my_gui_data.yes_no_cnt=0;
% Initialize the "Yes" counter
my_gui_data.yes_cnt=0;
% Initialize the "No" counter
my_gui_data.no_cnt=0;
% Initialize the "Yes / No " array
my_gui_data.yes_no=[];
% Initialize the "Yes / No " string
my_gui_data.yes_no_str='';
% Store the updated GUI data
guidata(gcf,my_gui_data);
% --- Outputs from this function are returned to the command line.
function varargout = yes_no_counter_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;
% --- Executes on button press in yes_btn.
function yes_btn_Callback(hObject, eventdata, handles)
% hObject handle to yes_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get the GUI data
my_gui_data=guidata(gcf);
% Increment the £Yes / No" counter
my_gui_data.yes_no_cnt=my_gui_data.yes_no_cnt+1
% Increment the "Yes" counter
my_gui_data.yes_cnt=my_gui_data.yes_cnt+1
% Store the "Yes" as "1" in the "Yes / No" array
my_gui_data.yes_no(my_gui_data.yes_no_cnt)=1
% Print the number of "Yes" and "No" in the first textbox
set(handles.text1,'string',['Yes= ' num2str(my_gui_data.yes_cnt) '; No= ' num2str(my_gui_data.no_cnt)])
% Build the cumulative "Yes / No" string
my_gui_data.yes_no_str=strcat(my_gui_data.yes_no_str,' Yes');
% Print the cumulative "Yes / No" string n the second textbox
set(handles.text2,'string',my_gui_data.yes_no_str)
% Store the updated GUI data
guidata(gcf,my_gui_data);
% --- Executes on button press in no_btn.
function no_btn_Callback(hObject, eventdata, handles)
% hObject handle to no_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get the GUI data
my_gui_data=guidata(gcf);
% Increment the "Yes / No" counter
my_gui_data.yes_no_cnt=my_gui_data.yes_no_cnt+1
% Increment the "No" counter
my_gui_data.no_cnt=my_gui_data.no_cnt+1
% Store the "Yes" as "-1" in the "Yes / No" array
my_gui_data.yes_no(my_gui_data.yes_no_cnt)=-1
% Print the number of "Yes" and "No" in the first textbox
set(handles.text1,'string',['Yes= ' num2str(my_gui_data.yes_cnt) '; No= ' num2str(my_gui_data.no_cnt)])
% Build the cumulative "Yes / No" string
my_gui_data.yes_no_str=strcat(my_gui_data.yes_no_str,' No');
% Print the cumulative "Yes / No" string n the second textbox
set(handles.text2,'string',my_gui_data.yes_no_str)
% Store the updated GUI data
guidata(gcf,my_gui_data);
The GUI
Hope this help.