通过popupmenu更新表matlab gui

时间:2016-01-01 10:35:55

标签: matlab user-interface

我正在编写一个包含popupmenu和table的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',)...

(我想从矩阵数据库中插入第1列第1列)

然而在这一点上没有任何作用。表格的标签是表格。

1 个答案:

答案 0 :(得分:0)

在您发布的代码中,所选弹出菜单项(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)来测试这种方法: - popupmenu标签:popupmenu1 - 表标签:uitable1 - 输入矩阵在GUI OpeningFcn中定义为一组随机数 - 弹出菜单中显示的字符串是OpeningFcn内置的 - 输入矩阵存储在GUI数据的句柄结构中

这是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

enter image description here

希望这有帮助。