GUI table, add/remove row buttons
Show older comments
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
Jan
on 23 Jul 2013
You have formatted the text as code and the code as text. I could not resist to swap this by editing. It looked too crude before.
James Hendren
on 23 Jul 2013
James Hendren
on 23 Jul 2013
Accepted Answer
More Answers (2)
James Hendren
on 23 Jul 2013
James Hendren
on 23 Jul 2013
0 votes
5 Comments
Because your code attempts to create all your uicontrols (your pushbuttons and table). GUIDE is set up so that these objects are already created whenever the GUI is run (this is one of the many advantages of using GUIDE), assuming that you went through and set all those properties when making the figure.
To migrate your code from your "from scratch" mfile to an mfile created whenever you save your GUI in guide, just copy and paste the code in your callbacks into the respective callbacks in the GUIDE-generated mfile. Everything else is already taken care of through the property settings you choose when using the GUIDE interface.
Alternatively, you could set these properties within the opening function of your GUI. But instead of using the form handle.myCtrl = uicontrol('Property',Value) You would use set(handles.myCtrl,'Property',Value)
James Hendren
on 23 Jul 2013
You can make uitables in GUIDE. The toggle button to select a uitable is the third from the bottom in the second column from the left-hand side of the window. It's directly below the listbox button and above the panel button.
Once you're placed your table, you can modify many of its properties through right click > Table Property Editor. The others can be found in the Property Inspector like any other uicontrol.
James Hendren
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.
Categories
Find more on App Building 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!