GUI table, add/remove row buttons

9 views (last 30 days)
James Hendren
James Hendren on 23 Jul 2013
I have this GUI that makes a table and uses a push button to add rows. I need help so I can remove rows using the push button as well. Also, I would like to know how I can change the number of columns so that it will be more than two. Thanks in advance.
function Add_Row_To_Table
%create a table:
handles.table1 = uitable('Data',{'a',false;'b' true; 'c',true},...
'ColumnFormat',{[],'logical'},...
'ColumnEditable',[false true],...
'CellEditCallback',@(h,e) disp([e.Indices(1) e.NewData]));
% create a pushbutton:
handles.pushbutton1 = uicontrol('Style','Pushbutton',...
'Units','Pixels',...
'Position',[150 350 80 40],...
'String','Add Row');
% create a pushbutton:
handles.pushbutton2 = uicontrol('Style','Pushbutton',...
'Units','Pixels',...
'Position',[50 350 80 40],...
'String','Remove Row')
%set the action of the pushbutton1 for when it is clicked
set(handles.pushbutton1,'Callback',{@AddRow,handles})
set(handles.pushbutton2,'Callback',{@RemoveRow,handles})
function AddRow(h,e,handles)
%get old data:
oldData = get(handles.table1,'Data');
nRows = size(oldData,1);
%generate a new row of data:
newRow = {char(97+nRows) logical(rem(nRows,2))};
%add new row to existing data
newData = [oldData;newRow];
set(handles.table1,'Data',newData)
%
%set the action of the pushbutton for when it is clicked
set(handles.pushbutton2,'Callback',{@RemoveRow,handles})
  3 Comments
James Hendren
James Hendren on 23 Jul 2013
function Add_Row_To_Table
%create a table:
handles.table1 = uitable('Data',{'a',false;'b' true; 'c',true},...
'ColumnFormat',{[],'logical'},...
'ColumnEditable',[false true],...
'CellEditCallback',@(h,e) disp([e.Indices(1) e.NewData]));
% create a pushbutton:
handles.pushbutton1 = uicontrol('Style','Pushbutton',...
'Units','Pixels',...
'Position',[150 350 80 40],...
'String','Add Row');
% create a pushbutton:
handles.pushbutton2 = uicontrol('Style','Pushbutton',...
'Units','Pixels',...
'Position',[50 350 80 40],...
'String','Remove Row')
%set the action of the pushbutton1 for when it is clicked
set(handles.pushbutton1,'Callback',{@AddRow,handles})
set(handles.pushbutton2,'Callback',{@RemoveRow,handles})
function AddRow(h,e,handles)
%get old data:
oldData = get(handles.table1,'Data');
nRows = size(oldData,1);
%generate a new row of data:
newRow = {char(97+nRows) logical(rem(nRows,2))};
%add new row to existing data
newData = [oldData;newRow];
set(handles.table1,'Data',newData)
%
%set the action of the pushbutton for when it is clicked
set(handles.pushbutton2,'Callback',{@RemoveRow,handles})
James Hendren
James Hendren on 23 Jul 2013
fixed the code, something isn't working for me out of your suggestion

Sign in to comment.

Accepted Answer

Evan
Evan on 23 Jul 2013
Edited: Evan on 23 Jul 2013
Here's a snippet of code I've used in the past for adding and subtracting rows. I modified it for use with a pushbutton. This assumes that you used GUIDE to create your GUI, but it can be modified if you're creating your interface with a standalone mfile.
function addButton_Callback(hObject,eventdata,handles)
oldDat = get(handles.myTable,'Data');
nRows = size(oldDat,1)
dat = cell(nRows+1,3);
dat(1:nRows,:) = oldDat;
set(handles.myTable,'Data',dat)
guidata(hObject,handles)
function subtractButton_Callback(hObject,eventdata,handles)
oldDat = get(handles.myTable,'Data');
nRows = size(oldDat,1)
dat = cell(nRows-1,3);
dat = oldDat(1:nRows-1,:);
set(handles.myTable,'Data',dat)
guidata(hObject,handles)
You might need to make some changes to fit your needs. At the moment, this assumes a table with 3 columns. The code you posted is at the moment unreadable, but if you format it using the "Code" button in the text editor we can comment specifically on it.

More Answers (2)

James Hendren
James Hendren on 23 Jul 2013
function Add_Row_To_Table
%create a table:
handles.table1 = uitable('Data',{'a',false;'b' true; 'c',true},...
'ColumnFormat',{[],'logical'},...
'ColumnEditable',[false true],...
'CellEditCallback',@(h,e) disp([e.Indices(1) e.NewData]));
% create a pushbutton:
handles.pushbutton1 = uicontrol('Style','Pushbutton',...
'Units','Pixels',...
'Position',[150 350 80 40],...
'String','Add Row');
% create a pushbutton:
handles.pushbutton2 = uicontrol('Style','Pushbutton',...
'Units','Pixels',...
'Position',[50 350 80 40],...
'String','Remove Row')
%set the action of the pushbutton1 for when it is clicked
set(handles.pushbutton1,'Callback',{@AddRow,handles})
set(handles.pushbutton2,'Callback',{@RemoveRow,handles})
function AddRow(h,e,handles)
%get old data:
oldData = get(handles.table1,'Data');
nRows = size(oldData,1);
%generate a new row of data:
newRow = {char(97+nRows) logical(rem(nRows,2))};
%add new row to existing data
newData = [oldData;newRow];
set(handles.table1,'Data',newData)
%
%set the action of the pushbutton for when it is clicked
set(handles.pushbutton2,'Callback',{@RemoveRow,handles})

James Hendren
James Hendren on 23 Jul 2013
also how come I cannot simply insert the above code into a GUI made with guide?
  5 Comments
James Hendren
James Hendren on 24 Jul 2013
I understand this....I just don't know to insert the code into the GUI without a callback for the table
Evan
Evan on 24 Jul 2013
Edited: Evan on 24 Jul 2013
Ohhh, okay. My bad. Since the actions only occur whenever you hit one of your pushbuttons, you wouldn't need to do anything with the callback for the table to add/remove rows. You would just paste the code into the callbacks of the pushbuttons, which should be automatically created when you save. The callback will then be added to your mfile.
Just for future reference, if you ever want to add a callback for your table in GUIDE, just rick click > View Callbacks. Then choose either CellEditCallback or CellSelectionCallback depending on what you need.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!