File Exchange Pick of the Week

Our best user submissions

MATLAB Basics: Adding a table to a GUI 43

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.

Note

Comments are closed.

43 CommentsOldest to Newest

Harold Figueroa replied on : 1 of 43
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 43
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 43
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 43
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 43
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 43
Malcolm, This suggestion has been entered into our internal enhancement request database. Thanks for the suggestion. Doug
Austin DeHart replied on : 7 of 43
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 43
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 43
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 43
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.
Waggy replied on : 13 of 43
Doug, I am investigating a way of loading the uitable with data from two variables. One is a 10x12 double however the second is a 12x13 cell. Is this achievable in any way? Many thanks Doug, Waggy
Doug replied on : 14 of 43
Waggy, You are going to have to decide how you want these odd shaped pieces to fit together. I recommend putting them all into one big cell, then setting that to the data as normal. Doug
Waggy replied on : 15 of 43
Doug, Thanks for your advice, i have merged the data into a cell however when i select the cell in Property Inspector - Data, it will not display the cell data. If i choose a double array i have in the Workspace it displays fine! Do i need to convert the data within the cell to allow uitable to accept it? Thanks Doug, Waggy
Doug replied on : 16 of 43
Waggy, Here is a very simple example: t = uitable('Data',{1 2 3; '1' '2' '3'}) This is a cell array with strings and numerics. It works fine. Doug
rossonera replied on : 17 of 43
Hello, Please, i have never used tables before in matlab and i am using matlab R2007b. Il like to know how to create a table. This is how its supposed to work---I have 2 GUI windows, one for collecting user data like name , gender, address etc and the secon GUI window is the one that should contain the table. I will like a case whereby a user inputs his/her personal details in the first GUI and on pressing "update button" from that first GUI window, it gets added to the table of the second GUI. As different other users key in their personal details and clicks on "update button", that table continues to be populated and updated
Doug replied on : 18 of 43
Rossonera, http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data This might help you! Doug
Ben replied on : 19 of 43
Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property inspector for when there are lots of adjustable parameters in the application. This works well when the parameters all have the same format, otherwise to make use of the column formatting & input features we can only make a "transposed" version (side scrolling, not so pretty). Having row formatting as an option would be nice. Are there any plans for this in future versions - or is there a better way to go about this?
Taylor replied on : 20 of 43
Doug, I have been using GUIDE to build a table that will need 1 column and some number of rows. So, I create a table in GUIDE, which by default has 2 columns and 4 rows, but when I open the Table Property Editor and try to delete the second column (bringing the total number of columns to 1), hitting "Apply" does not delete the column in the GUI, but instead causes the column to reappear in the Table Property Editor. This only happens when the table has two columns, i.e. if I have three columns, I can delete one of them. My application only needs one column and this will be a professional tool, so I really don't want to have a blank column just hanging there. Is this a known problem? Any workarounds? Note also that the same issue arises for rows. If I delete some number of rows so that the total number of rows is less than 4, then hitting "Apply" reverts the setup to 4 rows. Thanks. -Taylor
Taylor replied on : 21 of 43
Nevermind, I've figured it out. Looking through the background (GUIDE-exported) code, I found that the number of columns looks to be set by the column names and data. So, in my case, the code looks like:
h11 = uitable(...
'ColumnName',{  blanks(0); blanks(0)},...
'Data',{blanks(0) blanks(0);blanks(0) blanks(0);blanks(0) blanks(0);blanks(0) blanks(0)});
Note that I've cut out some of the unimportant code for readability. So, returning to the m-file that goes with the GUIDE fig file, I need only put the following line in the uitable's CreateFcn,
set(hObject,'ColumnName',blanks(0),'Data',{blanks(0);blanks(0);blanks(0)});
And voila, the table is initialized to be empty with 3 rows and 1 column.
Doug replied on : 22 of 43
@Taylor, Looks like you figured it out. The enhancement to allow this must have been implemented while I was not looking! I have deleted the earlier posts to clean this thread up since you figured it out! Doug
Vidar replied on : 23 of 43
Dear Doug, I'm trying to build a GUI with a 6 x 8 matrix that outputs to the workspace. I have managed to build a nice table with the inital values, column- and rowheaders that I wanted to have displayed using your tutorial. I also manage to get output to the commandwindow via the type of code used in the last part of your tutorial. However, I don't seem to get it to output the matrix from the GUI to the workspace. All help would be appreciated. Vidar
Doug replied on : 24 of 43
@Vidar, I think this would be a good video to watch. https://blogs.mathworks.com/videos/2010/02/12/advanced-getting-an-output-from-a-guide-gui/ Doug
Juan replied on : 25 of 43
Hi Doug, Nice video. Would it be possible to copy data from an excel sheet and paste these data in a table (doing ctrl+v or something like that)? Thanks, Juan
Rama replied on : 26 of 43
I have created a uitable using the GUIDE, and modifying the properties like 'RowName' programmatically(because it cant be fixed). If I give more than 4 rownames(say 6), the 6 rows are visible with the appropriate rownames. But the values of the 5th row onwards are not returned to the workspace. Please provide a solution for the same. Thanks, Rama
Doug replied on : 27 of 43
@Rama, Please send the shortest reproduction code that shows the behavior and we will take a look at it. doug
Rama replied on : 28 of 43
I got the answer for my problem from the following discussion. http:// Thanks for offering the help. - Rama
Ricardo replied on : 29 of 43
Hi Doug is there any way to import data from an excel file directly to a Table in GUI ? if so, can you tell me how do I do it please, I need some help. Thanks for the help.
Doug replied on : 30 of 43
@Ricardo, It will take two commands, one to load it in, one to place it in the table. Once put into a little function, it can seem like a single command though. Doug
Ricardo replied on : 31 of 43
Hi Doug Thanks for replied. But I still need a little help. How do I make the command for load the data and the command to place the data into the table ? In fact, if it possible, could you please provide me the code for this purpose ?
Doug replied on : 33 of 43
@Ricardo, This video shows how to set the value of data in a UITABLE. XLSREAD will get the data into MATLAB. Look in the Excel section of this blog to find videos on how to read the data in from Excel in MATLAB. Doug
Pardosi replied on : 34 of 43
Hello Doug. Im new to Matlab, using 2009a. My question, why is it if i try get the data from the table, it appears the quote sign ('') in each element even if i have set the format to be numeric in column option.. str2num doesnt work too.. Maybe filling the table with 'workspace data' will do the trick, but i want the table to be empty before user edit/input the table. thanks.
Chia replied on : 36 of 43
thank you so much doug..it works. then I want to display the graphic from network in nntool of matlab into gui again. best regards, Chia
Matthew replied on : 37 of 43
Hi Doug, First, your videos are always very useful! Keep up the good work! Second, the uitable doesn't allow the user to copy and paste data into the uitable cells from Excel. I would like to have this functionality, but am unsure how to accomplish it. I am aware that I could prompt the user to select an Excel file and read the data that way, but different users may not have to same Excel sheet formating. A simple copy and paste utility would work wonderfully. Any suggestions? Thanks!
Doug replied on : 38 of 43
@Matthew, I have put in a request for that functionality. Let us hope that the priority is high enough for it to happen soon! Doug
Matthew replied on : 39 of 43
Doug, Thank you for your prompt response! I was, however, able to come up with a suitable solution. I added a button to go along with the table, appropriately labeled "Paste". I added the following code to the button callback:
raw_data = importdata('-pastespecial');

set(handles.input_table,'Data',raw_data)
Of course, I added code between those two lines to make sure the user is entering numeric data and in the correct size dimensions! :-P Thanks!
Paul replied on : 41 of 43
is there a way to create a dynamic table? I am using a Daq interface and i want my values obtained from my daq function to be constantly updated on the table, is this possible?
Aiya replied on : 42 of 43
Hi Doug. I've created a table in GUI, 10rows x 3 columns. I want to have the 1st column to be editable by user, 2nd & 3rd columns are computed using data from 1st column. For example, col1 = A, col2 = 3*A, col3 = A*col2. How do I do this? Many thanks!
eng.fa replied on : 43 of 43
I want to make GUI and it will include pop-up-menu, and table, this pop up menu will have choise and for each choise it will have table. I mean if we choose A"first choise" ,,,table 1 will be display, AND when we choose B (2nd choise) .. table 2 will shown and table 1 hide any idea??