Search code examples
matlabuser-interfaceinsertrowmatlab-uitable

Matlab first row uitable


I have a question regarding inserting data from edit into a table. As it is now, a new row appears with the data that I wrote in the textboxes. My problem is the first row.. it is blank.

Thanks in advance

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

Solution

  • Try creating the table with something like:

    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!
    

    which initially looks like this (zero rows):

    zero_rows

    Then each time you add a new row, you can write:

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

    Here is the table after adding one row:

    one_row