【问题标题】:Matlab GUI: How to store the order and results of a push buttonsMatlab GUI:如何存储按钮的顺序和结果
【发布时间】:2016-04-14 02:43:59
【问题描述】:

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

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

谢谢。

【问题讨论】:

    标签: matlab user-interface button push store


    【解决方案1】:

    要计算您可能在 GUI 中定义的“是”和“否”的数量:

    • “是”的计数器
    • “否”的计数器

    跟踪您可能定义的“是”和“否”的顺序

    • “是”和“否”的“全局”计数器
    • 一个数组,根据按下的按钮顺序将“是”存储为例如“1”,将“否”存储为“-1”
    • 一个字符串,根据按下的按钮顺序存储“是”“否”

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

    那么变量是:

    • 设置在“是”和“否”pushbutton callback
    • 存储在GUI data的结构中(同样是guidata函数

    在“是”和“否”pushbutton callback 中,您还可以打印“是”和“否”按钮被按下的次数以及它们在两个text box 中的顺序。

    您还可以使用存储“是”/“否”作为1-1 的数组来绘制图表。

    下面是用于测试解决方案的GUIyes_no_counter的编码;在 GUI 中:

    • “是”pushbutton tag= `yes_btn
    • “否”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);
    

    图形用户界面

    希望对您有所帮助。

    【讨论】:

    • 非常感谢您的帮助。我不想显示计数器,这就是我发现它如此困难的原因。我只是缺少 guidata(hObject , 句柄);在我编辑了结构之后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 2017-12-13
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多