我有一个带有2个按钮的简单GUI,一个说是,另一个说不。
如何记录每次按下的次数,更重要的是记录顺序?
感谢。
答案 0 :(得分:2)
计算"是"和"不"您可以在GUI中定义:
跟踪"是"的顺序。和"不"你可以定义
以上定义的所有变量都可以在GUI OpeningFcn
中初始化,然后存储在GUI data
的结构中(使用guidata
结尾。
变量是:
pushbutton callback
GUI data
的结构中(再次使用guidata
函数在"是"和"不" pushbutton callback
您还可以打印"是"的次数。和"不"已经按下了按钮,它们的顺序分为两个text box
。
您还可以使用您已存储"是"的阵列。 /"否"例如,1
和-1
绘制图表。
在下文中,用于测试解决方案的GUI yes_no_counter
的编码;在GUI中:
pushbutton tag
=`yes_btn pushbutton tag
= no_btn
text box tag
=" text1" text box tag
=&#34; text2&#34; <强> 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);
GUI
希望得到这个帮助。