Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Inside the MATLAB Desktop

June 2nd, 2008

Tables in MATLAB with uitable


I’d like to welcome guest blogger Denis Hanson from the GUI Building team. Denis will occasionally be blogging here on the Inside the MATLAB Desktop blog.

In R2008a, we added the uitable control, as well as support for this new control in Guide. The uitable component enables the display of data in a two dimensional table. You can use the Property Inspector or the new Table Property Editor to get and set uitable object property values (to open the Property Inspector type inspect at the command prompt; to open the Table Property Editor, right-click your uitable in Guide and select Table Property Editor…).

The Table Property Editor makes it easy to define the layout of a table inside Guide. The Property Inspector could be used to achieve the same results, though it’s not targeted to uitable. Lets look at using the Table Property Editor to setup a uitable.

Table properties fall into four natural categories, which are presented on separate panels. Here’s a listing of each of the Table Property Editor’s panels, and what you can do on each:

  • Columns – set column names, format, size, and editability
  • Rows – set row names
  • Data – select data to display in the table
  • Colors – set colors and control row striping

Notice also that at the top of each panel is a help section. The text in the panel will change to reflect the option that is currently selected on the property panel.

Let’s look at each of the panels in detail.

Columns Panel

The Column Headers section of this panel lets you control how and if column headers are displayed. The Column Definitions section of this panel lets you tweak properties for each column and also allows the insertion, removal and rearranging of columns.

The Format column in the Column Definitions table specifies how the data should be formated for viewing. Here are each of the options and what they do:

  • Let MATLAB Choose - MATLAB chooses a format that most closely matches the underlying column data. For example, if the data in the column is doubles, the Numeric display option will be used.
  • Choice List - allows you to enter a series of series of values that will be displayed in a popupmenu. This option is especially useful if the column is editable and you want to restrict the values that they user can enter.
  • Format - lets you choose from a pre-defined list of formats for displaying numeric data. The options are the same as those found in the format function.

Rows Panel

The Rows panel lets you define how row headers are displayed. Row headers are shown in an additional column at the left edge of the table. They do not contain table data values. If shown, they typically display the row number, but you can enter a custom name for each row.

Data Panel

The Data panel lets you populate the table with data from workspace variables. A table row will be created for each row in your data set and a table column will be created for each column in your data set. Once you have saved the data into the table, the table contains a copy of the data. The table data is not linked to the workspace variable so further modifications to the workspace variable will not be reflected in the table.

The values you set here are used to populate the table when your figure is displayed. This is useful if you have canned data that does not change. If your data is not fixed, you should select the Do not set data value and leave the table empty option. You will then need to load table data programmatically.

Colors Panel

The Colors panel lets you define the background, foreground, and row-striping colors used to paint the cells in the table. When row-striping is on the table paints odd and even rows using different background colors. The striping effect makes it easier for a user to scan the data in a table row, especially if there are many rows of data.

This was a world-wind tour of the new uitable control - in a future post, we’ll show you how to use the new control in a real application.

Let us know what you think about uitable!


-by Denis Hanson, The MathWorks

9 Responses to “Tables in MATLAB with uitable”

  1. Oliver A. Chapman, P.E. replied on :

    Denis,

    Thanks for the column. But, I’m having trouble following the beginning. I’ve made some GUIs & untables before, but never with Guide. So, I follow you instructions: I type “inspect” and fired up Guide. But, I don’t see my “uitable” in Guide. Besides that, Guide seems to default into some generic figure, not a GUI.

    What am I missing?

  2. Ken replied on :

    Hi Oliver,

    Make sure to start up Guide first, then create a uitable from within Guide (rather than creating on from the command line first). When Guide first starts up, you’ll have a blank figure window on which you can drag UI controls.

    -Ken

  3. Moran replied on :

    Hi Ken/Denis,

    Thanks! This addition is very useful.

    However, there is one thing that seems to be missing - can you get the selected row or column identifiers? This would be really useful when using uitable to present a database from which the user can extract specific rows.

    I see that ‘evendata’ returns the selected indices, but is there a way to get this as an object property?

    Thanks!

  4. Denis replied on :

    Hi Moran,

    The current version of uitable does not contain properties for selectedRow and selectedColumn. These properties are at the top of our list of enhancements for uitable.

    You can use the values in CellSelectionCallback’s eventdata to track the selections as they occur, saving the selection indices into workspace variables.

    -Denis

  5. Liang Zhimo replied on :

    thank you for your column,but I still have a little trouble on it:when I use a table in a form which is used to input a matrix, I don’t know how to fix the column or row size of the table by command line (not inspector).Could you give me some advise?

  6. Denis replied on :

    Hi Liang,

    To control the column widths from the command line you use the set command. Here is an example that sets the first column to 100 pixels, the second column to 60 pixels, then autosizes the third column.

    set(uitable_handle, ‘ColumnWidth’, {100 60 ‘auto’})

    To make all columns in a table use autosizing, use

    set(uitable_handle, ‘ColumnWidth’, ‘auto’)

    As these examples show, the value for ColumnWidth is either a 1–by-n cell array or ‘auto’. The width used for ‘auto’ is the greater of (a) the width of the column header, and (b) 75 pixels.

    There is no property to control row size.

  7. bswang replied on :

    I use a uitable in guide. The uitable works well on the matlab environment. But when i build the mytest.m into the mytest.exe, the uitable doesn’t work because the data in the uitable can’t be displayed. why?

  8. Mike replied on :

    bswang,

    Depending on where your data is coming from, it may not be available from your standalone program (e.g. importing from a text or mat file that is located somewhere relative to your inside-MATLAB session, etc). Or, if you have multiple MATLABs/MCRs installed, the runtime environment may be different than what you expect.

    What you should do is dump the data to the console to verify that the data is actually available inside your program. If that works, you should open a help ticket with tech support so they can help you resolve the UI issue.

  9. Etienne replied on :

    Hello,

    Is it possible to change the background color of a particular cell?

Leave a Reply


Inside the MATLAB Desktop is written by the MATLAB Interface teams.

Team picture
  • Etienne: Hello, Is it possible to change the background color of a particular cell?
  • Ken: No problem Mark…glad to help. Enjoy your font!
  • Mark Andrews: Ken, thanks for replying @3 and giving such clear instructions on how to get this to work.
  • Steve Eddins: Hi Ken, Every year or so I try to find a programming font that would really make me happy. A couple of...
  • Ken: @Jiro: Andale Mono looks nice! I especially like the rendering of the letter “g”.
  • Ken: Hi Mark, The issue your referring to (see the bug report here) was fixed in version 7.5 (R2007b). The problem...
  • Jiro: Ken, That’s a nice font. I use “Andale Mono” which has a similar look. I also like the clear...
  • Queffurust: This is my “cleanup” ; shortcut clc % clear Command Window close all hidden % deletes all...
  • Mark Andrews: Ken, should this work with version 7.0 (R14) on Windows? My favorite font is Consolas, but MATLAB seems...
  • Mark Feldman: Like Bryan (#38) and Kim (#40), I strongly request that you add native vi key bindings to the MATLAB...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics