Matlab GUI:如何存储按钮的顺序和结果

Matlab GUI: How to store the order and results of a push buttons

我有一个带有 2 个按钮的简单 GUI,一个说是,另一个说不是。

如何记录每个按钮被按下的次数,更重要的是顺序?

谢谢。

要计算 "Yes" 和 "No" 的数量,您可以在 GUI 中定义:

  • "Yes"
  • 的计数器
  • "No"
  • 的计数器

为了跟踪您可能定义的 "Yes" 和 "No" 的顺序

  • "global" 计数器用于 "Yes" 和 "No"
  • 一个数组,其中根据已按下的按钮顺序存储 "Yes" 例如“1”和 "No" 作为“-1”
  • 根据按下的按钮
  • 顺序存储的字符串"Yes""No"

所有上面定义的变量都可以在 GUI OpeningFcn 中初始化,然后存储在 GUI data 的结构中(通过使用 guidata 函数。

那么变量是:

  • 设置在"Yes"和"No"pushbutton callback
  • 存储在GUI data的结构中(再次用guidata函数

在 "Yes" 和 "No" pushbutton callback 中,您还可以打印 "Yes" 和 "No" 按钮被按下的次数及其序列分为两个 text box.

您还可以使用将 "Yes" / "No" 存储为 1-1 的数组来绘制图形等。

下面使用GUI的编码yes_no_counter来测试解决方案;在 GUI 中:

  • "Yes" pushbutton tag= `yes_btn
  • "No" pushbutton 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);

GUI

希望对您有所帮助。