0

我有一个关于将编辑中的数据插入表格的问题。就像现在一样,出现了一个新行,其中包含我在文本框中写入的数据。我的问题是第一行..它是空白的。

提前致谢

function tablerows
% Test dynamic addition of rows
close all
clc
 figure
emptyRow = {'','',''};
tableData = emptyRow;

g=uitable('ColumnEditable', true(1,3), ...
'Data',tableData,'CellEditCallback',@editCallback);
uicontrol('style','pushbutton',...
      'position' ,[100 0300 100 50],...
      'callback', @kort)

w=uicontrol('style','edit',...
      'position' ,[400 220 100 30]);
q=uicontrol('style','edit',...
      'position' ,[400 320 100 30]) ;   
e=uicontrol('style','edit',...
      'position' ,[400 120 100 30]);

function kort (hObjects,eventdata)
    old=get(g,'data')
    newrow={get(q,'string') get(w,'string') get(e,'string')}
    new=[old;newrow]
   set(g,'data',new)
    if get(g,'data')=={'', '' ,''}
        set(g,'data',newrow)
    else 
        set(g,'data',new)
    end

    end
end
4

1 回答 1

1

尝试使用以下内容创建表:

figure('Menubar','none', 'Position',[400 400 300 200])

h = uitable('Units','normalized', 'Position',[0 0 1 1], ...
    'ColumnName',{'1','2','3'}, 'Data',cell(0,3));    % <-- the important part!

最初看起来像这样(零行):

zero_rows

然后每次添加新行时,您可以编写:

d = get(h, 'Data');
d(end+1,:) = {'aaa', 123, true};   % append a new row 
set(h, 'Data',d)

这是添加一行后的表格:

one_row

于 2014-07-07T10:56:34.327 回答