Stuart’s MATLAB Videos

Watch and Learn

MATLAB Basics: Adding a table to a GUI 27

Posted by Doug Hull,

MATLAB GUI developers often want to have a mini-spreadsheet in their GUI. This can be done with a new widget added to the set of controls available in GUIDE. Now with UITABLE you can add that capability. This video shows how to add a table for display, then makes it editable and adds a callback such that MATLAB code is run whenever a cell in the table is edited. The data is not linked to the variables in the workspace, so this callback shows you how to pull data out of the table whenever it is updated.
 

27 CommentsOldest to Newest

Harold Figueroa replied on : 1 of 27
So are these tables only numeric, or can we use a mixed type cell array to populate them? Also I am currently using 7.5 and see that these tables are available but undocumented. It seems like in this version the 'data' is a java object.
Brett replied on : 2 of 27
Doug, nice post. One thing, though: it occurs to me that you haven't shown us how to use/share the value of the variable DATA (from the data in the table) outside of the workspace of the cellEditCallback workspace. There are several ways to do it--addressing the ways, and their relative merits, would make a nice follow-up blog!
Ryan replied on : 3 of 27
Brett, I agree. Given that the uitable is a new addition to MATLAB it would be great to see more discussion about various access methods.
Doug replied on : 4 of 27
Harold, UITable was undocumented, but available, for some releases in the past. Some features like this are available, but undocumented, for various reasons. Sometimes they are there so that we can use them in making tools for MATLAB. These features are undocumented because we need the flexibility to change the interface for a while. If you find undocumented features, you need to be aware that the interface can change or be removed in future releases. -Doug
Malcolm Lidierth replied on : 5 of 27
As far as access is concerned, the undocumented uitable of old provided cut and paste options from a uicontextmenu but the new version does not. Is that something that will be addressed in future releases?
Doug replied on : 6 of 27
Malcolm, This suggestion has been entered into our internal enhancement request database. Thanks for the suggestion. Doug
Austin DeHart replied on : 7 of 27
Hello Doug, I have a gui with a uitable that works well when you hit enter after each cell edit, but when you hit tab after editing a cell's contents, the CellEditCallback is not executed. Is there a fix for this? Thanks, Austin DeHart
Keerthi Nagaraj replied on : 9 of 27
Hey Doug, My question follows the previous post here. I am trying to edit the cells of a table. It works well if I hit tab after entering data. However, if I choose the contents of a cell by mouse-click-and-drag, and edit it, unless I click in another cell, the CellEditCallback is not called. I tend to edit the last cell, and then directly press a button that processes the data. So, I end up missing the data on the last cell I edit. How can I rectify this? By the way, I am sincerely grateful for the most educative and helpful posts. I even considered developing GUI, solely due to your videos. Thank you very much, Keerthi
Doug replied on : 10 of 27
Keerthi, Our developers are aware of that behavior. A change in behavior is likely in an upcoming release. Doug
Hector Mejía replied on : 11 of 27
Hi, I have Matlab 7.5.0 (R2007b) and I need to create a table on an interface, but in the GUI doesn't exist a botton to create it, do I have to install something to obtain that botton in my GUI Thanks.
Yana replied on : 13 of 27
Hi, Doug! I created uitable in my GUI app by Guide, and set some properies by Table Property Editor, to be exact, for Columns set [numeric numeric numeric CHOICE_LIST logical numeric] 1 2 3 4 5 6 And I would like to input my data to table. Beforehand I created my data as cell (4x6), where {i,4} is cell array of strings for corresponding 4th column (pop-up menu) K>> handles.visualdata.params ans = Columns 1 through 5 [ 20] [10] [ 300] {1x29 cell} [0] [ 5] [ 5] [ 50] {1x10 cell} [1] [ 2] [ 1] [ 10] {1x9 cell} [1] [733788] [ 1] [733796] {1x7 cell} [0] Column 6 [ 20] [ 5] [ 2] [733788] K>> set(handles.uitable_params,'Data',handles.visualdata.params); ??? Error using ==> set Values within a cell array must be numeric, logical, or char -------------------- Doug, could you tell me if I have posibility to set different popup menu for each row?
Tzu-hao replied on : 15 of 27
Dear Doug Thank you for this useful demonstration. One issue I ran into while setting up a table is that I found if we pre-assign values into the table, when the values are later edited, it will align to the right. Otherwise it will align to the left. Is there a way to enforce the edited data to always align to the center (since I want to assign values to only a portion of the boxes). Thanks Tzu-hao
dhull replied on : 16 of 27
@Tzu-hao Are you finding that the property that controls justification is changing? Is it different for strings vs numerical data? Doug
Sunita replied on : 17 of 27
Problem making checkbox and pup-up box editable: I created uitable in GUI app containing checkbox, pup-box and numeric format and enabled the 'Editable' checkobx in Table Property editor. I can edit all formats in the GUI, however if i add an additional row though my program in m-file the numberic cell is still editable but the checkbox and pop-ups are not. How can i make these format editable. Regards, Sunita
Sunita replied on : 19 of 27
Hi Doug, I did the following; I added a row to expand the table as below; data = get(handles.Data_table,'Data'); newRow = cat(1,data,cell(1,size(data,2))); set(handles.Data_table,'Data',newRow); this adds a row but the checkbox is not editable. So i added below lines but still i did not work; Editable =[true,true,true,true,true,true,true]; set(handles.Data_table, 'ColumnEditable',Editable) this problem is only for new added rows. Regards, Sunita
slumberk replied on : 22 of 27
Hi Doug. I have a popup menu and a table. Popup menu is to select a generator that will be used by the user. For example if the user wants to use 2 generators, then the user may select '2 'on the popup menu. If the user select 2, then the table will be issued a 2 by 3 table. If users select three generators, then the table will be issued a 3 by 3 table. If users select four generators, then the table will be issued a 4 by 3 table and so on. My question is how i want this situation applies? What I mean is how to produce a 2 by 3 table when the user select '2' and 3 by 3 table when the user select '3' and so on? For your info, My project is about economic dispatch. I have to build GUI using 4 generators and each of the generator have limit. So i have to make 2 uitables. So each generator user selected, then the user have to insert limit( in megawatt) for each generator(which is another table) This is the example of my table(4 by 3): Alpha Beta Gamma C1 C2 C3 C4 The equation for the generator is : C1 = (Alpha) + (Beta)P + (Gamma)P^2 C2 = (Alpha) + (Beta)P + (Gamma)P^2 C3 = (Alpha) + (Beta)P + (Gamma)P^2 C4 = (Alpha) + (Beta)P + (Gamma)P^2 This is a table for the limit of each generator: Lower Upper C1 C2 C3 C4 The form of this table will be : Lower < C1 < Upper Lower < C2 < Upper Lower < C3 < Upper Lower < C4 < Upper So when the user select '2', then it will popup the 2 by 3 table. How i want to do it? What is the code for it and how i want to call the table data into the pushbuttons? Well i already make 2 uitable figures and i already set the table properties like the row name n column name. But how i want to make it only 2 row only? Please do help me on this.
carl angelo cruz replied on : 24 of 27
I have same problem with @sunita singh. I have a choicelist in my table. But when I add another row to expand the table, the choicelist still editable but it says "NaN". How can I change this format? thank you.
David replied on : 25 of 27
Please I would like to know if it was possible to add columns with new results from a loop into a uitable such as is done with plots using the hold on command. Thank you
Doug replied on : 26 of 27
@David, You can moddify the value of the 'data' in the UITABLE to reflect as many values and columns as you wish.
mua phe lieu dong replied on : 27 of 27
Wow, that's what I was seeking for, what a data! existing here at this web site, thanks admin of this site.