【问题标题】:Updating table via popupmenu matlab gui通过弹出菜单 matlab gui 更新表
【发布时间】:2016-11-18 04:58:47
【问题描述】:

我正在编写一个包含弹出菜单和表格的 matlab GUI。 我的目标是从弹出菜单中选择选项,它会根据它填充表格。

% --- Executes during object creation, after setting all properties.
function Item_List_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Item_List (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

handles.Item_list_value = get(hObject,'Value');

在这一行之后,我收到了我从弹出菜单中选择的选项编号,我想从矩阵中获取相同的列编号并将其插入到表格中。

类似:

set(handles.Table,'data',)...

(我想将矩阵数据库中的第一列插入第一列)

但是此时没有任何效果。表格的标签是Table。

【问题讨论】:

    标签: matlab user-interface


    【解决方案1】:

    在您发布的代码中,设置所选弹出菜单项(handles.Item_list_value = get(hObject,'Value');)的索引的说明写在弹出菜单CreateFcn中,除非您没有发布正确的代码,这意味着您可以在弹出菜单的创建过程中,只获取一次所选项目的索引。

    要插入/更新uitable 的内容,您应该在popupmenu callback 中编写以下过程:

    • 使用get函数获取选中的弹出菜单项的索引
    • 通过get函数获取表数据(数据以(n x m)数组形式返回)
    • 将使用 popupmenu 选择的输入矩阵列插入到适当的表格列中:输入矩阵在popupmenu callback 中应该是“可见的”。在您的问题中没有指定如何管理输入矩阵,一种可能性是使用guidata 函数将其存储(例如从输入文件中读取之后)到 GUI 数据中
    • 更新表数据(使用set函数)

    我构建了一个小型 GUI (updating_table_via_popup) 来测试这种方法: - 弹出菜单标签:popupmenu1 - 表标签:uitable1 - 输入矩阵在 GUI OpeningFcn 中定义为一组随机数 - 要在弹出菜单中显示的字符串内置于OpeningFcn - 输入矩阵存储在GUI数据的handles结构中

    这是 GUI 的代码,参考。到updating_table_via_popup_OpeningFcnpopupmenu1_Callback 函数,以实现建议的解决方案。

    function varargout = updating_table_via_popup(varargin)
    % UPDATING_TABLE_VIA_POPUP MATLAB code for updating_table_via_popup.fig
    %      UPDATING_TABLE_VIA_POPUP, by itself, creates a new UPDATING_TABLE_VIA_POPUP or raises the existing
    %      singleton*.
    %
    %      H = UPDATING_TABLE_VIA_POPUP returns the handle to a new UPDATING_TABLE_VIA_POPUP or the handle to
    %      the existing singleton*.
    %
    %      UPDATING_TABLE_VIA_POPUP('CALLBACK',hObject,eventData,handles,...) calls the local
    %      function named CALLBACK in UPDATING_TABLE_VIA_POPUP.M with the given input arguments.
    %
    %      UPDATING_TABLE_VIA_POPUP('Property','Value',...) creates a new UPDATING_TABLE_VIA_POPUP or raises the
    %      existing singleton*.  Starting from the left, property value pairs are
    %      applied to the GUI before updating_table_via_popup_OpeningFcn gets called.  An
    %      unrecognized property name or invalid value makes property application
    %      stop.  All inputs are passed to updating_table_via_popup_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 updating_table_via_popup
    
    % Last Modified by GUIDE v2.5 01-Jan-2016 19:55:39
    
    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @updating_table_via_popup_OpeningFcn, ...
                       'gui_OutputFcn',  @updating_table_via_popup_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 updating_table_via_popup is made visible.
    function updating_table_via_popup_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 updating_table_via_popup (see VARARGIN)
    
    % Choose default command line output for updating_table_via_popup
    handles.output = hObject;
    
    % Update handles structure
    guidata(hObject, handles);
    
    % Define the input matrix
    n_row=3;
    n_col=5;
    data_base=reshape([1:n_row*n_col]',n_col,n_row)';
    % Get the GUI data
    my_gui_data=guidata(gcf);
    % Store the input matrix into the GUI data
    my_gui_data.data_base=data_base;
    % Update GUI data
    guidata(gcf,my_gui_data);
    % Initialize table to "0"
    set(handles.uitable1,'data',zeros(n_row,n_col))
    % Build the popupmenu strings
    str{1}='- Select col. to insert -'
    for i=2:n_col+1
       str{i}=['Insert Col. #' num2str(i-1)]
    end
    % Assign the strings to the popupmenu
    set(handles.popupmenu1,'string',str)
    
    
    % UIWAIT makes updating_table_via_popup wait for user response (see UIRESUME)
    % uiwait(handles.figure1);
    
    
    % --- Outputs from this function are returned to the command line.
    function varargout = updating_table_via_popup_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 selection change in popupmenu1.
    function popupmenu1_Callback(hObject, eventdata, handles)
    % hObject    handle to popupmenu1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
    %        contents{get(hObject,'Value')} returns selected item from popupmenu1
    
    % Get the GUI data
    my_gui_data=guidata(gcf);
    % Get the index of the selected popupmenu item (-1 to account for the first
    % string)
    sel_idx=get(handles.popupmenu1,'value')-1;
    % Get the table data
    table_data=get(handles.uitable1,'data');
    % Insert the input matrix column selected with popupmenu into the proper
    % table column
    table_data(:,sel_idx)=my_gui_data.data_base(:,sel_idx)
    % Updata the table data
    set(handles.uitable1,'data',table_data);
    
    
    % --- Executes during object creation, after setting all properties.
    function popupmenu1_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to popupmenu1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: popupmenu controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多