Matlab Logo 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.

MATLAB UITABLE Tutorial 2

Contents

Accessing Table Data within GUI Callbacks

Let’s say you have the following GUI:

MATLAB UITABLE Tutorial 2

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.

MATLAB UITABLE Tutorial 2

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).

MATLAB UITABLE Tutorial 2

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.

MATLAB UITABLE Tutorial 2

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:

MATLAB UITABLE Tutorial 2

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:

MATLAB UITABLE Tutorial 2

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!

MATLAB UITABLE Tutorial 2

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