Home > m > gui > ltpdaRepoGUI > muitable.m

muitable

PURPOSE ^

UITABLE creates a two dimensional graphic uitable component in a figure window.

SYNOPSIS ^

function [table, container] = muitable(varargin)

DESCRIPTION ^

 UITABLE creates a two dimensional graphic uitable component in a figure window.
     UITABLE creates a 1x1 uitable object using default property values in
     a figure window.

     UITABLE(numrows,numcolumns) creates a uitable object with specified
     number of rows and columns.

     UITABLE(data,columnNames) creates a uitable object with the specified
     data and columnNames. Data can be a cell array or a vector and
     columnNames should be cell arrays.

     UITABLE('PropertyName1',value1,'PropertyName2',value2,...) creates a
     uitable object with specified property values. MATLAB uses default
     property values for any property not explicitly set. The properties
     that user can set are: ColumnNames, Data, GridColor, NumColumns,
     NumRows, Position, ColumnWidth and RowHeight.

     UITABLE(figurehandle, ...) creates a uitable object in the figure
     window specified by the figure handle.

     HANDLE = UITABLE(...) creates a uitable object and returns its handle.

     Properties:

     ColumnNames:  Cell array of strings for column names.
     Data:         Cell array of values to be displayed in the table.
     GridColor:    string, RGB vector.
     NumColumns:   int specifying number of columns.
     NumRows:      int specifying number of rows.
     Parent:       Handle to figure or uipanel. If not specified, it is gcf.
     Position:     4 element vector specifying the position.
     ColumnWidth:  int specifying the width of columns.
     RowHeight:    int specifying the height of columns.

     Enabled:      Boolean specifying if a column is enabled.
     Editable:     Boolean specifying if a column is editable.
     Units:        String - pixels/normalized/inches/points/centimeters.
     Visible:      Boolean specifying if table is visible.
     DataChangedCallback - Callback function name or handle.


     Examples:

     t = uitable(3, 2);

     Creates a 3x2 empty uitable object in a figure window.

     f = figure;
     t = uitable(f, rand(5), {'A', 'B', 'C', 'D', 'E'});

     Creates a 5x5 uitable object in a figure window with the specified
     data and the column names.

     data = rand(3);
     colnames = {'X-Data', 'Y-Data', 'Z-Data'};
     t = uitable(data, colnames,'Position', [20 20 250 100]);

     Creates a uitable object with the specified data and column names and
     the specified Position.

     See also AWTCREATE, AWTINVOKE, JAVACOMPONENT, UITREE, UITREENODE

 Adapted for LTPDA by M Hewitson
 - a direct copy of MATLAB's uitable.m

 $Id: muitable.m,v 1.3 2008/02/26 08:54:43 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [table, container] = muitable(varargin)
0002 
0003 % UITABLE creates a two dimensional graphic uitable component in a figure window.
0004 %     UITABLE creates a 1x1 uitable object using default property values in
0005 %     a figure window.
0006 %
0007 %     UITABLE(numrows,numcolumns) creates a uitable object with specified
0008 %     number of rows and columns.
0009 %
0010 %     UITABLE(data,columnNames) creates a uitable object with the specified
0011 %     data and columnNames. Data can be a cell array or a vector and
0012 %     columnNames should be cell arrays.
0013 %
0014 %     UITABLE('PropertyName1',value1,'PropertyName2',value2,...) creates a
0015 %     uitable object with specified property values. MATLAB uses default
0016 %     property values for any property not explicitly set. The properties
0017 %     that user can set are: ColumnNames, Data, GridColor, NumColumns,
0018 %     NumRows, Position, ColumnWidth and RowHeight.
0019 %
0020 %     UITABLE(figurehandle, ...) creates a uitable object in the figure
0021 %     window specified by the figure handle.
0022 %
0023 %     HANDLE = UITABLE(...) creates a uitable object and returns its handle.
0024 %
0025 %     Properties:
0026 %
0027 %     ColumnNames:  Cell array of strings for column names.
0028 %     Data:         Cell array of values to be displayed in the table.
0029 %     GridColor:    string, RGB vector.
0030 %     NumColumns:   int specifying number of columns.
0031 %     NumRows:      int specifying number of rows.
0032 %     Parent:       Handle to figure or uipanel. If not specified, it is gcf.
0033 %     Position:     4 element vector specifying the position.
0034 %     ColumnWidth:  int specifying the width of columns.
0035 %     RowHeight:    int specifying the height of columns.
0036 %
0037 %     Enabled:      Boolean specifying if a column is enabled.
0038 %     Editable:     Boolean specifying if a column is editable.
0039 %     Units:        String - pixels/normalized/inches/points/centimeters.
0040 %     Visible:      Boolean specifying if table is visible.
0041 %     DataChangedCallback - Callback function name or handle.
0042 %
0043 %
0044 %     Examples:
0045 %
0046 %     t = uitable(3, 2);
0047 %
0048 %     Creates a 3x2 empty uitable object in a figure window.
0049 %
0050 %     f = figure;
0051 %     t = uitable(f, rand(5), {'A', 'B', 'C', 'D', 'E'});
0052 %
0053 %     Creates a 5x5 uitable object in a figure window with the specified
0054 %     data and the column names.
0055 %
0056 %     data = rand(3);
0057 %     colnames = {'X-Data', 'Y-Data', 'Z-Data'};
0058 %     t = uitable(data, colnames,'Position', [20 20 250 100]);
0059 %
0060 %     Creates a uitable object with the specified data and column names and
0061 %     the specified Position.
0062 %
0063 %     See also AWTCREATE, AWTINVOKE, JAVACOMPONENT, UITREE, UITREENODE
0064 %
0065 % Adapted for LTPDA by M Hewitson
0066 % - a direct copy of MATLAB's uitable.m
0067 %
0068 % $Id: muitable.m,v 1.3 2008/02/26 08:54:43 hewitson Exp $
0069 %
0070 
0071 
0072 %   Copyright 2002-2006 The MathWorks, Inc.
0073 %   $Revision: 1.3 $  $Date: 2008/02/26 08:54:43 $
0074 
0075 %   Release: R14. This feature will not work in previous versions of MATLAB.
0076 
0077 % Setup and P-V parsing
0078 
0079 error(javachk('awt'));
0080 error(nargoutchk(0,2,nargout));
0081 
0082 parent = [];
0083 numargs = nargin;
0084 
0085 datastatus=false; columnstatus=false;
0086 rownum = 1; colnum = 1; % Default to a 1x1 table.
0087 position = [20 20 200 200];
0088 combo_box_found = false;
0089 check_box_found = false;
0090 
0091 import com.mathworks.hg.peer.UitablePeer;
0092 
0093 if (numargs > 0 && isscalar(varargin{1}) && ishandle(varargin{1}) && ...
0094         isa(handle(varargin{1}), 'figure'))
0095     parent = varargin{1};
0096     varargin = varargin(2:end);
0097     numargs = numargs - 1;
0098 end
0099 
0100 if (numargs > 0 && isscalar(varargin{1}) &&  ishandle(varargin{1}))
0101     if ~isa(varargin{1}, 'javax.swing.table.DefaultTableModel')
0102         error('MATLAB:uitable:UnrecognizedParameter', ['Unrecognized parameter: ', varargin{1}]);
0103     end
0104     data_model = varargin{1};
0105     varargin = varargin(2:end);
0106     numargs = numargs - 1;
0107 
0108 elseif ((numargs > 1) && isnumeric(varargin{1}) && isnumeric(varargin{2}))
0109     if(isnumeric(varargin{1}) && isnumeric(varargin{2}))
0110         rownum = varargin{1};
0111         colnum = varargin{2};
0112 
0113         varargin = varargin(3:end);
0114         numargs = numargs-2;
0115     else
0116         error('MATLAB:uitable:InputMustBeScalar', 'When using UITABLE numrows and numcols have to be numeric scalars.')
0117     end
0118 
0119 elseif ((numargs > 1) && isequal(size(varargin{2},1), 1) && iscell(varargin{2}))
0120     if (size(varargin{1},2) == size(varargin{2},2))
0121         if (isnumeric(varargin{1}))
0122             varargin{1} = num2cell(varargin{1});
0123         end
0124     else
0125         error('MATLAB:uitable:MustMatchInfo', 'Number of column names must match number of columns in data');
0126     end
0127     data = varargin{1};     datastatus        = true;
0128     coln = varargin{1+1};   columnstatus      = true;
0129 
0130     varargin = varargin(3:end);
0131     numargs = numargs-2;
0132 end
0133 
0134 for i = 1:2:numargs-1
0135     if (~ischar(varargin{i}))
0136         error('MATLAB:uitable:UnrecognizedParameter', ['Unrecognized parameter: ', varargin{i}]);
0137     end
0138     switch lower(varargin{i})
0139         case 'data'
0140             if (isnumeric(varargin{i+1}))
0141                 varargin{i+1} = num2cell(varargin{i+1});
0142             end
0143             data        = varargin{i+1};
0144             datastatus  = true;
0145 
0146         case 'columnnames'
0147             if(iscell(varargin{i+1}))
0148                 coln            = varargin{i+1};
0149                 columnstatus    = true;
0150             else
0151                 error('MATLAB:uitable:InvalidCellArray', 'When using UITABLE Column data should be 1xn cell array')
0152             end
0153 
0154         case 'numrows'
0155             if (isnumeric(varargin{i+1}))
0156                 rownum = varargin{i+1};
0157             else
0158                 error('MATLAB:uitable:NumrowsMustBeScalar', 'numrows has to be a scalar')
0159             end
0160 
0161         case 'numcolumns'
0162             if (isnumeric(varargin{i+1}))
0163                 colnum = varargin{i+1};
0164             else
0165                 error('MATLAB:uitable:NumcolumnsMustBeScalar', 'numcolumns has to be a scalar')
0166             end
0167 
0168         case 'gridcolor'
0169             if (ischar(varargin{i+1}))
0170                 gridcolor = varargin{i+1};
0171             else if (isnumeric(varargin{i+1}) && (numel(varargin{i+1}) == 3))
0172                     gridcolor = varargin{i+1};
0173                 else
0174                     error('MATLAB:uitable:InvalidString', 'gridcolor has to be a valid string')
0175                 end
0176             end
0177 
0178         case 'rowheight'
0179             if (isnumeric(varargin{i+1}))
0180                 rowheight = varargin{i+1};
0181             else
0182                 error('MATLAB:uitable:RowheightMustBeScalar', 'rowheight has to be a scalar')
0183             end
0184 
0185         case 'parent'
0186             if ishandle(varargin{i+1})
0187                 parent = varargin{i+1};
0188             else
0189                 error('MATLAB:uitable:InvalidParent', 'parent must be a valid handle')
0190             end
0191 
0192         case 'position'
0193             if (isnumeric(varargin{i+1}))
0194                 position = varargin{i+1};
0195             else
0196                 error('MATLAB:uitable:InvalidPosition', 'position has to be a 1x4 numeric array')
0197             end
0198 
0199         case 'columnwidth'
0200             if (isnumeric(varargin{i+1}))
0201                 columnwidth = varargin{i+1};
0202             else
0203                 error('MATLAB:uitable:ColumnwidthMustBeScalar', 'columnwidth has to be a scalar')
0204             end
0205         otherwise
0206             error('MATLAB:uitable:UnrecognizedParameter', ['Unrecognized parameter: ', varargin{i}]);
0207     end
0208 end
0209 
0210 % ---combo/check box detection--- %
0211 if (datastatus)
0212     if (iscell(data))
0213         rownum = size(data,1);
0214         colnum = size(data,2);
0215         combo_count =0;
0216         check_count = 0;
0217         combo_box_data   = num2cell(zeros(1, colnum));
0218         combo_box_column = zeros(1, colnum);
0219         check_box_column = zeros(1, colnum);
0220         for j = 1:rownum
0221             for k = 1:colnum
0222                 if (iscell(data{j,k}))
0223                     combo_box_found = true;
0224                     combo_count = combo_count + 1;
0225                     combo_box_data{combo_count} = data{j,k};
0226                     combo_box_column(combo_count ) = k;
0227                     dc = data{j,k};
0228                     data{j,k} = dc{1};
0229                 else
0230                     if(islogical(data{j,k}))
0231                         check_box_found = true;
0232                         check_count = check_count + 1;
0233                         check_box_column(check_count) = k;
0234                     end
0235                 end
0236             end
0237         end
0238     end
0239 end
0240 
0241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
0242 % Check the validity of the parent and/or create a figure.
0243 if isempty(parent)
0244     parent = gcf; % Get the current figure. Create one if not available
0245 end
0246     
0247 if ( columnstatus && datastatus )
0248     if(size(data,2) ~= size(coln,2))
0249         error('MATLAB:NeedSameNumberColumns', 'Number of columns in both Data and ColumnNames should match');
0250     end
0251 elseif ( ~columnstatus && datastatus )
0252     for i=1:size(data,2)
0253         coln{i} = num2str(i);
0254     end
0255     columnstatus = true;
0256 elseif ( columnstatus && ~datastatus)
0257     error('MATLAB:uitable:NoDataProvided', 'No Data provided along with ColumnNames');
0258 end
0259 
0260 if (~exist('data_model', 'var'))
0261     data_model = javax.swing.table.DefaultTableModel;
0262 end
0263 if exist('rownum', 'var')
0264     data_model.setRowCount(rownum);
0265 end
0266 if exist('colnum', 'var')
0267     data_model.setColumnCount(colnum);
0268 end
0269 
0270 table_h= UitablePeer(data_model);
0271 
0272 % We should have valid data and column names here.
0273 if (datastatus), table_h.setData(data); end;
0274 if (columnstatus), table_h.setColumnNames(coln); end;
0275 
0276 if (combo_box_found),
0277     for i=1:combo_count
0278         table_h.setComboBoxEditor(combo_box_data(i), combo_box_column(i));
0279     end
0280 end
0281 if (check_box_found),
0282     for i = 1: check_count
0283         table_h.setCheckBoxEditor(check_box_column(i));
0284     end
0285 end
0286 
0287 % pass the specified parent and let javacomponent decide its validity.
0288 [obj, container] = javacomponent(table_h, position, parent);
0289 % javacomponent returns a UDD handle for the java component passed in.
0290 table = obj;
0291 
0292 % Have to do a drawnow here to make the properties stick. Try to restrict
0293 % the drawnow call to only when it is absolutely required.
0294 flushed = false;
0295 if exist('gridcolor', 'var')
0296     pause(.1); drawnow;
0297     flushed = true;
0298     table_h.setGridColor(gridcolor);
0299 end
0300 if exist('rowheight', 'var')
0301     if (~flushed)
0302         drawnow;
0303     end
0304     table_h.setRowHeight(rowheight);
0305 end
0306 if exist('columnwidth', 'var')
0307     table_h.setColumnWidth(columnwidth);
0308 end;
0309 
0310 
0311 
0312 % Add a predestroy listener so we can call cleanup on the table.
0313 % addlistener(table, 'ObjectBeingDestroyed', {@componentDelete});
0314 
0315 function componentDelete(src, evd)                                     %#ok
0316 % Clean up the table here so it disengages all its internal listeners.
0317 src.cleanup;

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003