MATLAB GUI Tutorial - UITABLE Part 1, How to Display Data
27 May 2009 Quan Quach 7 comments 6,310 views
With the release of MATLAB 2008b, you are now able to add tables to a GUI. In the past, there was no easy way to display your data in tabular form. With the UITABLE component, displaying your data in tabular form is easy, and most importantly, looks great!

Contents
- Adding a Table to Your GUI using GUIDE
- Displaying Data on the Table
- Adding Column and Row Labels
- Modifying your Table through the m-file
- Next Time
- Links and Downloads
Adding a Table to Your GUI using GUIDE
Within the GUIDE framework, you can add a table to your GUI using the following icon from the toolbar:
.
Here’s what the GUI will look like within GUIDE:

Displaying Data on the Table
We are going to populate the UITABLE component with data by pushing the “Populate Table” button. Thus, we’re going to need to add some code to the pushbutton’s callback. In the populate_pushbutton callback, we use the following code:
% --- Executes on button press in populate_pushbutton. function populate_pushbutton_Callback(hObject, eventdata, handles) % hObject handle to populate_pushbutton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %first, create a data matrix that has 5 columns, 4 rows myData = rand(5,4); %now populate the table with the above values set(handles.uitable1,'data',myData );
Now, let’s run the GUI and push the button!

A neat feature is that the table is smart enough to fill out the table according to the size of the data matrix that you feed it. So if I had done the following instead:
myData = rand(100,100);
The table would incorporate the use of scroll bars, as shown below.

Adding Column and Row Labels
A good way to spruce up your table is to add row and column labels. This helps differentiate your data and makes it easy to identify. Within the GUIDE framework, we can modify the labels by first bringing up the Property Inspector for the UITABLE. This can be done by double clicking the UITABLE component.

Now, if you click on any of the fields in the above picture, it will bring up the Table Property Editor. This is where you can add Row and Column labels. For example:

Make sure you click on the “Rows”, and that you select the “Show names entered below as the row headers” option. Finally, you just need to modify the names. Similarly, you can do the same for the columns.

Once you’re done with that. you should see the following:

And once you run your GUI, you can see the final result. A well labeled table that displays your data beautifully!

Modifying your Table through the m-file
Sometimes it’s easier to work from within the m-file framework, rather than the GUIDE framework. We could have done exactly what we did above programmatically through the m-file. In the opening function we could have done the following:
%store the row headers into a cell array rowHeaders = {'Blink','Dagger','Loves','MATLAB','!!!!!!'}; %set the row labels set(handles.uitable1,'RowName',rowHeaders); %do the same for the column headers columnHeaders = {'Quan','Daniel','Rob','Zane'}; set(handles.uitable1,'ColumnName',columnHeaders);
In this example, we assumed that we knew the dimensions of our table. If you don’t know the size of your table beforehand, then it can be difficult to apply data labels that are meaningful. By working through the m-file, you obtain more flexibility since you won’t have to go back and modify the .fig file every time you want to make a change. And if you are going to apply dynamic labeling, then working from the m-file is going to be much easier.
Next Time
Next time, we’re going to talk about how to work with manipulating the data within the table.
Links and Downloads
Download Source Files
The MathWorks Documentation for UITABLE
Cool Things You can do with UITABLE
Doug’s Video on UITABLE




