MATLAB GUI Tutorial - UITABLE Part 2, How To Access Table Data
31 May 2009 Quan Quach 9 comments 6,266 views
Last time, we learned how to display data onto a table. This time, we’re going to learn how to work with table data within a GUI framework. For example, say you wanted to take the contents of the table manipulate the data. This tutorial will explain how to do that, and much more.

Contents
- Accessing Table Data within GUI Callbacks
- Accessing only the Selected Data
- Next Time
- Links and Downloads
Accessing Table Data within GUI Callbacks
Let’s say you have the following GUI:

For simplicity sake, let’s assume that you would like to create a button that will add 3 to each of the entries of the table when the button is pressed. How would you go about doing this? It’s actually quite straightforward. Let’s take a look at the callback for the add button:
function add_pushbutton_Callback(hObject, eventdata, handles) % hObject handle to add_pushbutton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %get the table data tableData = get(handles.uitable1,'data'); %add 3 to the table tableData = tableData + 3; %update the table set(handles.uitable1,'data',tableData);
So now, when I press the “Add 3″ button, it adds 3 to the table! This is just a simple example to show how to extract the data from the UITABLE, and to perform an operation on it.

Accessing only the Selected Data
Now, let’s up the difficulty level a bit. Let’s say you selected a couple of cells that you want to sum, as shown in the image below (You can hold onto the CTRL button while clicking on individual cells to select multiple cells).

How would you go about doing this? Read on and all will be revealed.
Enabling CellSelectionCallback
The first thing we need to do is to enable the Cell Selection Callback. But first, why are we doing this? Enabling this callback will allow us to keep track of what cells are being selected on the table. You can do this by bringing up the Property Inspector for the UITABLE, and then clicking the following icon as shown in the image below.

If you did it correctly, your m-file should have been updated to include the following:
% --- Executes when selected cell(s) is changed in uitable1. function uitable1_CellSelectionCallback(hObject, eventdata, handles) % hObject handle to uitable1 (see GCBO) % eventdata structure with the following fields (see UITABLE) % Indices: row and column indices of the cell(s) currently selecteds % handles structure with handles and user data (see GUIDATA)
In addition, the CellSelectionCallback field should now be populated as shown in the image below:

Adding the Necessary Code
First, let’s create and initialize a variable to hold the table cell selection information. We will call this handles.selectedCells, and initialize it in the opening function.
% --- Executes just before uitable_tutorial_02 is made visible. function uitable_tutorial_02_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to uitable_tutorial_02 (see VARARGIN) %initialize this variable handles.selectedCells = [];
Next, we go to the uitable1_CellSelectionCallback, which is the callback that we just enabled.
% --- Executes when selected cell(s) is changed in uitable1. function uitable1_CellSelectionCallback(hObject, eventdata, handles) % hObject handle to uitable1 (see GCBO) % eventdata structure with the following fields (see UITABLE) % Indices: row and column indices of the cell(s) currently selecteds % handles structure with handles and user data (see GUIDATA) %every time the cell selection changes, we update this data %eventdata stores the indices of the selected cells handles.selectedCells = eventdata.Indices; %update the gui data guidata(hObject, handles);
Adding the Selected Numbers Together
First, we’re going to add another button and a static text component to display the sum. The modified GUI looks like this:

Now, we need to write the callback for the button we just added:
% --- Executes on button press in sumNumbers_pushbutton. function sumNumbers_pushbutton_Callback(hObject, eventdata, handles) % hObject handle to sumNumbers_pushbutton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %get the number of rows and columns [rows,columns] = size(handles.selectedCells); %get the data from the UITABLE tableData = get(handles.uitable1,'data'); %initialize the sum sum = 0; %loop through each selected cell and keep a running sum %can anyone thing of a better way to do this? for x=1:rows sum = sum +tableData(tableIndices(x,1),tableIndices(x,2)); end %display the sum on the GUI set(handles.sum_text,'String',num2str(sum))
And there you have it, now you can select any number of cells, and then sum up the value of the contents!

Next Time
Next time, we’re going to talk about some of the cool features of the UITABLE that we have not yet discussed, including different data types within the UITABLE.
Links and Downloads
Download Source Files
The MathWorks Documentation for UITABLE
Cool Things You can do with UITABLE
Doug’s Video on UITABLE
9 Responses to “MATLAB GUI Tutorial - UITABLE Part 2, How To Access Table Data”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


Well done,
Keep the good work up
Thanks
Hello,
first of all thank you for this site, your tutorials are very helpful.
I would be very glad if you could help me also with these two questions:
I try to generate two GUIs which should have the following function:
in the first GUI you can enter the number of rows and columns and in the second GUI should be a table of the given size by the entered numbers of rows and columns.
My problem is that I don´t know how to program the variable size of the table. And I don´t know where to write it in the given GUIDE source code, I think it has to be in the Opening Function, but how could I do this?
And how do I get the user entered data into the workspace? I programmed the Callbacks for the Edit Text and the Table and the entered data are shown in the command window but they are not in the workspace.
Thank you.
Kind regards
Debbi
Debbi,
If this is still helpful to you, I think I know how to answer your questions.
Assuming that you already read the tutorials here on how to link your two GUIs together, your problem is how to resize the table. If you are just trying to read your variables from the one GUI into the other, lets call these variables x and y, then look at the following copy/pasted from part 1 of this tutorial:
So with you, you could make a matrix of zeros to make your table the right size, by doing:
Granted, this will fill your table with zeros unless you move on to fill it up with other things, but it gets you the right size. As far as I can tell, the uitable just resizes itself, so you don’t really need to declare its size in advance. It just molds to whatever size array you toss in there.
To get the data returned to the workspace, you need to tell your functions to return the data, and when you call the function, give it a place to store the data.
So do it like this:
That stuff in the brackets to the left of the = are your outputs. If you set these to what you want them to be somewhere in your function code, they will be sent back to your workspace when you run Iamafunction, *BUT* you have to put them someplace. So if you run it like:
That runs your function with the values input1 = 1, input2 = 2, and then will save output1 and output2 to the variables x and y in your workspace when it’s done.
Hope that helps!
@Debbi and Brittany: If you use
then you will have an empty table (rather than filled with zeros).
@Quan: If you have a uitable in the GUI holding a sufficient amount of data for there to be a scroll bar, is there any way to access the position of the scroll bar? For example, is there a way to have two tables with the same number of columns such that scrolling to the right on one table also scrolls to the right on the other?
Also, is there a way to make the column headers user-editable?
Thanks.
@Andy:
Thanks for the tip! I didn’t think about using cell to make empty arrays, but I just started doing that yesterday in another program.
I’m not sure about the scrollbar, sorry.
As for user editable column headers, I have a similar application, in which my user reads in data sets with an unknown number of columns, all with headers. I perform basic statistics on all of the columns and display the results in a table, with the header of each column as the label in the table. So the basic principle is the same, I don’t know the column headers ahead of time and must add them in the code.
Where labels is a cell array of your titles. I *think* it has to be a row vector for it to work, but it might work with a column, but I won’t swear to it. I also have had no need to try it with multiple lines.
From here it’s a simple use of text boxes or inputdlg or whatever your preferred method is to get the titles for said cell array, then toss it in!
Hello everybody…
i have another question, which is pretty related to the selection issue… i have a table, where just whole lines shall be select by clicking anywhere in the line - if i could select the cells manually by some sort of command i was able to do it, but i dont know how to select them…
Thanx in advance if someone knows the answer!
Hi Quan,
There seems to be a bug in MATLAB (2008a or earlier versions) when using uitable. When we compile a GUI into a standalone application using MATLAB’s “deploytool”, in the resulting .exe file the table content will be completely blank even if there’s data. I don’t know if you have a way around it.
I am sorry to see that this blog is closing. I wish I had the energy to take it over, but I am still doing my PhD and have very limited time to spare. This is the best MATLAB blog I’ve ever seen online. (Especially in comparison with the not-helpful-at-all mathworks official forum)
dear debbi
first of lal u have to create variables
then to save the variables in some place using the save command
then use load to retrive the data
i hope it helps u
Hi everybody !!! Can you tell how to change the value in 1 cell. Ex: row Blink colum Quan of QUAN’s table is “1″ I wanna change it become “5″. How can I do that ? Please tell me