Solutions for Matlab Simulink and RTW
Friday, 21 November 2008
 
  Home arrow Tutorials arrow Matlab GUI Tables  
Home Consulting Tutorials Downloads Matlab Snippets Code Candy Contact Links
Matlab GUI Tables Print E-mail
Written by Administrator   
Thursday, 28 April 2005

Here is a simple cut at providing an easy to use method for using Java tables in Matlab. First of all we need to wrap up the basic uitable object provided by Matlab. It is too Javaish. Lets make it look like matlab.

We need to provide three things to the java table.

  1. A method to get data from a column and row location
  2. A method to set data from a column and row location
  3. A method to render data to the screen and read text data from a cell when it changes.
To do 2 and 3 we can supply function handles to the modified table that do the job.

The implementation and example useage of the modified table is below.
 


% XTARGETS_UITABLE
% 
% t = xtargets_uitable(rows, cols, get, set, pattern)
%
% Arguments
%   rows    numbers of rows
%   cols    number of cols
%   get     function handle for aquiring data to the table.
%           val = get(row, col)
%   set     function handle for setting data to the table.
%           set(row, col, val)
%   pattern sprintf/sscanf pattern for converting between data
%           and representation in the table. When the table
%           calls the get function it uses sprintf and the
%           pattern to convert the data. Before the table
%           calls set it uses scanf to convert the string
%           back to data you can use.

% Copyright Brad Phelan (C) 2005 under the LGPL license
% http://xtargets.com
function t = xtargets_uitable(rows, cols, get, set, pattern)

d_init = cell(rows, cols); for r = 1:rows
for c = 1:cols
d_init{r,c} = sprintf(pattern,get(r,c)); end end t = uitable(rows,cols); t.Data = d_init; t.Position = [20 20 250 100]; t.DataChangedCallback = @change_fcn; out = @get_data; function out = get_data
out = data; end function change_fcn(src, ev)
row = ev.getEvent.getFirstRow; col = ev.getEvent.getColumn; d = src.TableModel.getValueAt(row,col); set(row+1,col+1,sscanf(d,pattern)); end end

 

% USE_TABLE
%
% A simple example of using the table model to modify data in 
% a nested function workspace. The data is in double format
% so we use the '%g' sscanf pattern to render and read the
% data from the table cells.

% Copyright Brad Phelan (C) 2005 under the LGPL license
% http://xtargets.com
function use_table
close all; data = rand(4); % Create a uitable with a simple matlab data model. Specify % that the conversion class will be %g. t = xtargets_uitable(4,4, @getfun, @setfun, '%g'); function val = getfun( row, col)
val = data(row,col); end function setfun( row, col, val)
data(row,col) = val; end
    %Fix for JTable forcus bug : see http://www.mycgiserver.com/~Kleopatra/swing/table/merlinedit.html
  table = t.getTable;
table.putClientProperty('terminateEditOnFocusLost', java.lang.Boolean.TRUE);

% uicontrol to display the data model on the command line uicontrol('string','push me','position',[300 0 100 50], ... 'callback',@disp_data); function disp_data(src,ev)
disp(data); end end
 

Last Updated ( Thursday, 07 July 2005 )
< Prev   Next >
Sponsored by

Sole Central

Your one stop shop for Birkenstock shoes and sandles.