0

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
Amro
  • 123,847
  • 25
  • 243
  • 454
  • does [that](http://stackoverflow.com/questions/23306439/matlab-uitable-row-generation-from-user-input/23309315#23309315) help? – Robert Seifert Jul 07 '14 at 10:30
  • had it that way before, must have missed adding the four spaces when I copied it to here. EDIT: this is the error i get: Undefined function 'eq' for input arguments of type 'cell'. Error in tablerows/kort (line 27) if get(g,'data')=={'', '' ,''} Error while evaluating uicontrol Callback – user3796906 Jul 07 '14 at 10:39

1 Answers1

1

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

Amro
  • 123,847
  • 25
  • 243
  • 454