GUI table, add/remove row buttons

5 次查看(过去 30 天)
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 个评论
James Hendren
James Hendren 2013-7-23
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 2013-7-23
fixed the code, something isn't working for me out of your suggestion

请先登录,再进行评论。

采纳的回答

Evan
Evan 2013-7-23
编辑:Evan 2013-7-23
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.

更多回答(2 个)

James Hendren
James Hendren 2013-7-23
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 2013-7-23
also how come I cannot simply insert the above code into a GUI made with guide?
  5 个评论
James Hendren
James Hendren 2013-7-24
I understand this....I just don't know to insert the code into the GUI without a callback for the table
Evan
Evan 2013-7-24
编辑:Evan 2013-7-24
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by