GUI table, add/remove row buttons
조회 수: 9 (최근 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
채택된 답변
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.
댓글 수: 0
추가 답변 (2개)
James Hendren
2013년 7월 23일
댓글 수: 5
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 Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!