Uitables and widgets question

I have 2 questions.
1) Can checkboxes be implemented into the UItable when building a gui.
2) If so, can rows and checkboxes be added dynamically to the UItable.

 Accepted Answer

Tom
Tom on 25 Jun 2013
Edited: Tom on 25 Jun 2013
1) Yes
2) Yes
you can change the column format of the table at any point, using the ColumnFormat field of the uitable:
T = uitable('Data',{'a',false;'b' true},...
'ColumnFormat',{[] , 'numeric'},...
'ColumnEditable',[false true]);
pause(1)
set(T,'ColumnFormat',{[],'logical'})

More Answers (4)

James Hendren
James Hendren on 25 Jun 2013
Is your sample for question 1 or 2?

1 Comment

Tom
Tom on 25 Jun 2013
Edited: Tom on 25 Jun 2013
2 I suppose, it's not specific to GUIDE - you can run it in the command line or paste it into a script and run it. It shows a table being created, and then the second column being converted from numeric data to logical checkboxes.

Sign in to comment.

James Hendren
James Hendren on 25 Jun 2013
I am not getting checkboxes when I run it
James Hendren
James Hendren on 25 Jun 2013
I meant literal checkboxes from the GUIDE program

14 Comments

You can use the CellEditCallback of the uitable to let you run functions when the checkbox is clicked on in the same way as you could for the checkbox UI control.
(by the way, it will be clearer for people to read this if you reply to comments rather than replying with a new answer)
ok, so what about dynamically adding rows?
Just change the size of the data.
set(table_handle,'Data',data_variable)
if you're using GUIDE, table_handle will probably be handles.table1, or something similar.
Well I want the number of columns set, but I need to variably add the number of rows while operating the gui itself
Yes, and you can do that by resetting the data in the table. Are you using a callback to do this? E.g. Pushing a button adds an extra row to the table.
No. I was not. Could you give a sample please?
I would like a push button to do this.
function Add_Row_To_Table
%create a table:
handles.table1 = uitable('Data',{'a',false;'b' 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');
%set the action of the pushbutton for when it is clicked
set(handles.pushbutton1,'Callback',{@AddRow,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)
I'm having trouble combining the two because its creating a nested function
Paste that whole thing into a blank .m file and save it - it works for me
Where did you find all the commands for this? I have alot of code to remake using a gui and I am not very familiar with gui's at all.
I just picked it up over time. Be aware that I didn't create that code using GUIDE, I did it manually but used the same handles format that GUIDE uses. You might want to watch the 'Creating a GUI using GUIDE' video in the MATLAB help documentation
I have, but there is alot left out from the guide. For instance, I could not insert a checkbox into the table. Is there another source you would suggest?
To do it in GUIDE, you right click on the table, select Table Property Editor, then in the Columns tab there is an option to select what data format each column will take: make it 'logical'.

Sign in to comment.

James Hendren
James Hendren on 26 Jun 2013
Also, how could I install another push button "Remove Row" to delete all new created rows. Is there a way to implement add/remove buttons with GUIDE?

Categories

Find more on Develop Apps Using App Designer 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!