GUI table, add/remove row buttons

조회 수: 9 (최근 30일)
James Hendren
James Hendren 2013년 7월 23일
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