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

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 %   Copyright 2002-2006 The MathWorks, Inc.
0066 %   $Revision: 1.1.8.18 $  $Date: 2006/11/29 21:53:13 $
0067 
0068 %   Release: R14. This feature will not work in previous versions of MATLAB.
0069 
0070 % Setup and P-V parsing
0071 
0072 error(javachk('awt'));
0073 error(nargoutchk(0,2,nargout));
0074 
0075 parent = [];
0076 numargs = nargin;
0077 
0078 datastatus=false; columnstatus=false;
0079 rownum = 1; colnum = 1; % Default to a 1x1 table.
0080 position = [20 20 200 200];
0081 combo_box_found = false;
0082 check_box_found = false;
0083 
0084 import com.mathworks.hg.peer.UitablePeer;
0085 
0086 if (numargs > 0 && isscalar(varargin{1}) && ishandle(varargin{1}) && ...
0087         isa(handle(varargin{1}), 'figure'))
0088     parent = varargin{1};
0089     varargin = varargin(2:end);
0090     numargs = numargs - 1;
0091 end
0092 
0093 if (numargs > 0 && isscalar(varargin{1}) &&  ishandle(varargin{1}))
0094     if ~isa(varargin{1}, 'javax.swing.table.DefaultTableModel')
0095         error('MATLAB:uitable:UnrecognizedParameter', ['Unrecognized parameter: ', varargin{1}]);
0096     end
0097     data_model = varargin{1};
0098     varargin = varargin(2:end);
0099     numargs = numargs - 1;
0100 
0101 elseif ((numargs > 1) && isnumeric(varargin{1}) && isnumeric(varargin{2}))
0102     if(isnumeric(varargin{1}) && isnumeric(varargin{2}))
0103         rownum = varargin{1};
0104         colnum = varargin{2};
0105 
0106         varargin = varargin(3:end);
0107         numargs = numargs-2;
0108     else
0109         error('MATLAB:uitable:InputMustBeScalar', 'When using UITABLE numrows and numcols have to be numeric scalars.')
0110     end
0111 
0112 elseif ((numargs > 1) && isequal(size(varargin{2},1), 1) && iscell(varargin{2}))
0113     if (size(varargin{1},2) == size(varargin{2},2))
0114         if (isnumeric(varargin{1}))
0115             varargin{1} = num2cell(varargin{1});
0116         end
0117     else
0118         error('MATLAB:uitable:MustMatchInfo', 'Number of column names must match number of columns in data');
0119     end
0120     data = varargin{1};     datastatus        = true;
0121     coln = varargin{1+1};   columnstatus      = true;
0122 
0123     varargin = varargin(3:end);
0124     numargs = numargs-2;
0125 end
0126 
0127 for i = 1:2:numargs-1
0128     if (~ischar(varargin{i}))
0129         error('MATLAB:uitable:UnrecognizedParameter', ['Unrecognized parameter: ', varargin{i}]);
0130     end
0131     switch lower(varargin{i})
0132         case 'data'
0133             if (isnumeric(varargin{i+1}))
0134                 varargin{i+1} = num2cell(varargin{i+1});
0135             end
0136             data        = varargin{i+1};
0137             datastatus  = true;
0138 
0139         case 'columnnames'
0140             if(iscell(varargin{i+1}))
0141                 coln            = varargin{i+1};
0142                 columnstatus    = true;
0143             else
0144                 error('MATLAB:uitable:InvalidCellArray', 'When using UITABLE Column data should be 1xn cell array')
0145             end
0146 
0147         case 'numrows'
0148             if (isnumeric(varargin{i+1}))
0149                 rownum = varargin{i+1};
0150             else
0151                 error('MATLAB:uitable:NumrowsMustBeScalar', 'numrows has to be a scalar')
0152             end
0153 
0154         case 'numcolumns'
0155             if (isnumeric(varargin{i+1}))
0156                 colnum = varargin{i+1};
0157             else
0158                 error('MATLAB:uitable:NumcolumnsMustBeScalar', 'numcolumns has to be a scalar')
0159             end
0160 
0161         case 'gridcolor'
0162             if (ischar(varargin{i+1}))
0163                 gridcolor = varargin{i+1};
0164             else if (isnumeric(varargin{i+1}) && (numel(varargin{i+1}) == 3))
0165                     gridcolor = varargin{i+1};
0166                 else
0167                     error('MATLAB:uitable:InvalidString', 'gridcolor has to be a valid string')
0168                 end
0169             end
0170 
0171         case 'rowheight'
0172             if (isnumeric(varargin{i+1}))
0173                 rowheight = varargin{i+1};
0174             else
0175                 error('MATLAB:uitable:RowheightMustBeScalar', 'rowheight has to be a scalar')
0176             end
0177 
0178         case 'parent'
0179             if ishandle(varargin{i+1})
0180                 parent = varargin{i+1};
0181             else
0182                 error('MATLAB:uitable:InvalidParent', 'parent must be a valid handle')
0183             end
0184 
0185         case 'position'
0186             if (isnumeric(varargin{i+1}))
0187                 position = varargin{i+1};
0188             else
0189                 error('MATLAB:uitable:InvalidPosition', 'position has to be a 1x4 numeric array')
0190             end
0191 
0192         case 'columnwidth'
0193             if (isnumeric(varargin{i+1}))
0194                 columnwidth = varargin{i+1};
0195             else
0196                 error('MATLAB:uitable:ColumnwidthMustBeScalar', 'columnwidth has to be a scalar')
0197             end
0198         otherwise
0199             error('MATLAB:uitable:UnrecognizedParameter', ['Unrecognized parameter: ', varargin{i}]);
0200     end
0201 end
0202 
0203 % ---combo/check box detection--- %
0204 if (datastatus)
0205     if (iscell(data))
0206         rownum = size(data,1);
0207         colnum = size(data,2);
0208         combo_count =0;
0209         check_count = 0;
0210         combo_box_data   = num2cell(zeros(1, colnum));
0211         combo_box_column = zeros(1, colnum);
0212         check_box_column = zeros(1, colnum);
0213         for j = 1:rownum
0214             for k = 1:colnum
0215                 if (iscell(data{j,k}))
0216                     combo_box_found = true;
0217                     combo_count = combo_count + 1;
0218                     combo_box_data{combo_count} = data{j,k};
0219                     combo_box_column(combo_count ) = k;
0220                     dc = data{j,k};
0221                     data{j,k} = dc{1};
0222                 else
0223                     if(islogical(data{j,k}))
0224                         check_box_found = true;
0225                         check_count = check_count + 1;
0226                         check_box_column(check_count) = k;
0227                     end
0228                 end
0229             end
0230         end
0231     end
0232 end
0233 
0234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
0235 % Check the validity of the parent and/or create a figure.
0236 if isempty(parent)
0237     parent = gcf; % Get the current figure. Create one if not available
0238 end
0239     
0240 if ( columnstatus && datastatus )
0241     if(size(data,2) ~= size(coln,2))
0242         error('MATLAB:NeedSameNumberColumns', 'Number of columns in both Data and ColumnNames should match');
0243     end
0244 elseif ( ~columnstatus && datastatus )
0245     for i=1:size(data,2)
0246         coln{i} = num2str(i);
0247     end
0248     columnstatus = true;
0249 elseif ( columnstatus && ~datastatus)
0250     error('MATLAB:uitable:NoDataProvided', 'No Data provided along with ColumnNames');
0251 end
0252 
0253 if (~exist('data_model', 'var'))
0254     data_model = javax.swing.table.DefaultTableModel;
0255 end
0256 if exist('rownum', 'var')
0257     data_model.setRowCount(rownum);
0258 end
0259 if exist('colnum', 'var')
0260     data_model.setColumnCount(colnum);
0261 end
0262 
0263 table_h= UitablePeer(data_model);
0264 
0265 % We should have valid data and column names here.
0266 if (datastatus), table_h.setData(data); end;
0267 if (columnstatus), table_h.setColumnNames(coln); end;
0268 
0269 if (combo_box_found),
0270     for i=1:combo_count
0271         table_h.setComboBoxEditor(combo_box_data(i), combo_box_column(i));
0272     end
0273 end
0274 if (check_box_found),
0275     for i = 1: check_count
0276         table_h.setCheckBoxEditor(check_box_column(i));
0277     end
0278 end
0279 
0280 % pass the specified parent and let javacomponent decide its validity.
0281 [obj, container] = javacomponent(table_h, position, parent);
0282 % javacomponent returns a UDD handle for the java component passed in.
0283 table = obj;
0284 
0285 % Have to do a drawnow here to make the properties stick. Try to restrict
0286 % the drawnow call to only when it is absolutely required.
0287 flushed = false;
0288 if exist('gridcolor', 'var')
0289     pause(.1); drawnow;
0290     flushed = true;
0291     table_h.setGridColor(gridcolor);
0292 end
0293 if exist('rowheight', 'var')
0294     if (~flushed)
0295         drawnow;
0296     end
0297     table_h.setRowHeight(rowheight);
0298 end
0299 if exist('columnwidth', 'var')
0300     table_h.setColumnWidth(columnwidth);
0301 end;
0302 
0303 % Add a predestroy listener so we can call cleanup on the table.
0304 % addlistener(table, 'ObjectBeingDestroyed', {@componentDelete});
0305 
0306 function componentDelete(src, evd)                                     %#ok
0307 % Clean up the table here so it disengages all its internal listeners.
0308 src.cleanup;

Generated on Fri 02-Nov-2007 19:39:27 by m2html © 2003