📄 tablegui.m
字号:
function out = tableGUI(varargin)
% TABLEGUI - Spreadsheet like display and edition of a generic 2D array. By generic it is
% mean that the array can be a numeric MxN matrix or a MxN cell array (with mixed
% number and text strings). This function imitates the table cells with edit boxes
% which may become slow if the number of elements required is large. However, it
% works fast for small matrices. If the default number of rows is exceeded a vertical
% slider is created. The slider works by changing the position of the table elements,
% which again may become slow if the default number of visible rows is large. Otherwise
% it works pretty fast.
%
% USAGE:
% OUT = TABLEGUI(varargin)
%
% Inputs are in property/value pairs. All properties are strings but the values are
% of different types depending on the case.
%
% PROPERTY VALUE TYPE
%
% 'array' It can be either an numeric matrix or a MxN numeric or cell array
% cell array.
% 'NumRows' Total number of rows to create. Use this when the integer
% 'array' option is not used, or when you want to
% create extra empty rows.
% 'NumCol' Total number of columns to create. Use this when integer
% the 'array' option is not used, but ignored if it was.
% 'MAX_ROWS' Number of visible rows. If NumRows > MAX_ROWS integer
% a vertical slider is created (DEF = 10)
% 'RowHeight' editboxes height in pixels (DEF = 20) integer
% 'ColWidth' editboxes width in pixels (DEF = 60) scalar or row vector
% If a vector is transmited, it must contains NumCol
% elements which set individual column widths.
% 'bd_size' space between editboxes width in pixels (DEF = 0) integer
% 'HorAlin' editboxes 'HorizontalAlignment' property. It can string
% be either 'left', 'center' or 'right' (DEF = 'center')
% 'HdrButtons' create a first row of buttons to hold column string - either '' or 'y'
% names. Give an empty string ('') if you don't want
% column names (DEF = 'Y').
% 'ColNames' Cell array of strings for column names. If not 1xN cell array of strings
% provided the columns are named 'A', 'B', ...
% 'RowNumbers' Add a first column with row numbers. Note that this string - either '' or 'y'
% column is set to 'inactive' and its not transmited
% on the output (DEF = '').
% 'RowNames' Add a column with row names. Note that this column 1xN cell array of strings
% is set to 'inactive' and its not transmited on the
% output (DEF = ''). Warning: do not abuse on the
% Names length. Added by Martin Furlan
% 'RowNamesWidth' Width of buttons containing the RowNames
% in pixels (DEF = 60) scalar
% 'checks' If = 'Y' it creates a vertical line of checkboxes string - either '' or 'y'
% This affects what is send as output. Only rows that
% have it's checkbox checked will be returned.
% 'FigName' Name that appears in the title bar (DEF = 'Table'). string
% 'position' Screen location to be used in the call to MOVEGUI string
% See doc of that function for valid position
% strings (DEF = 'east').
% 'modal' By default the window works in MODAL mode. Give an string - either '' or 'y'
% empty string ('') if you don't want it to be MODAL.
% In this later case the output OUT, if requested, will
% contain the figure handle but see more about this below.
%
% OUT - the output - contains different things depending whether or not the
% figure works in MODAL mode. If yes, OUT is a MxN cell array with the
% elements retrived from the contents of the edit boxes. Otherwise, OUT
% will contain the figure's handle. This handle has the 'UserData' property
% filled with a structure (called hand) which contains the handles of all
% uicontrols. Use this option if you want to interact with the TABLEGUI
% figure inside your own code.
%
% Examples:
% - Display a 12x6 numeric matrix with two extra blank rows appended to the end
% out = tableGUI('array',rand(12,6),'ColNames',{'1' '2' '3' '4' '5' '6'},'NumRows',14);
%
% - Create a cell array with the first column filled with the row number and use
% columns with different widths. Create also check boxes.
% zz=cell(4,5); zz(:,1) = num2cell(1:4)';
% out = tableGUI('array',zz,'ColNames',{'N','A','B','C','D'},'ColWidth',[20 60 60 60 60],'checks','y');
%
% - Create a similar table as in the previous example but with the row numbers option.
% out = tableGUI('array',cell(4,5),'RowNumbers','y','checks','y');
%
% - Display the Control Points of the Image Processing Toolbox example
% "Registering an Aerial Photo to an Orthophoto"
% load westconcordpoints % load some points that were already picked
% gcp = [base_points input_points];
% out = tableGUI('array',gcp,'RowNumbers','y','ColNames',{'Base Points - X','Base Points - Y',...
% 'Input Points - X','Input Points - Y'},'ColWidth',110,'FigName','GCP Table');
%
% - Create an empty 12x6 empty table
% out = tableGUI;
%
% Acknowledgment
% This function uses the parse_pv_pairs of John d'Errico
%
% AUTHOR
% Joaquim Luis (jluis@ualg.pt) 17-Feb-2006
%
% Revision
% 17-Sep-2006 - There was an error when only one check box existed.
%
% Addition
% 6-Dec-2006 - A column with row names added by Martin Furlan
% (martin.furlan@iskra-ae.com)
%
% 13-Mar-2007 - Added option to control the RowNames width.
%
hand.NumRows = 12; hand.NumCol = 6;
hand.MAX_ROWS = 10; hand.left_marg = 10;
hand.RowHeight = 20; hand.bd_size = 0;
hand.checks = ''; hand.HdrButtons = 'Y';
hand.HorAlin = 'center'; hand.FigName = 'Table';
hand.array = cell(hand.NumRows,hand.NumCol);
hand.modal = 'y'; hand.position = 'east';
hand.RowNumbers = ''; d_col = 0;
hand.RowNames = '';
if (nargin == 0) % Demo
hand.ColNames = {'A' 'B' 'C' 'D' 'E' 'F'};
hand.ColWidth = [50 80 80 80 80 50];
else
hand.ColNames = ''; hand.ColWidth = [];
hand.array = []; def_NumRows = hand.NumRows;
hand.RowNamesWidth = 60;
hand = parse_pv_pairs(hand,varargin);
if (~isempty(hand.array))
if (numel(hand.array) == 1 && numel(hand.array{1}) > 1)
error('The "array" argument must be a MxN cell array and not a {MxN} cell')
end
[NumRows,hand.NumCol] = size(hand.array);
if (~iscell(hand.array)) % We need as a cell array to be more general
hand.array = num2cell(hand.array);
end
if (NumRows < hand.NumRows && hand.NumRows ~= def_NumRows) % Extra rows requested
hand.array = [hand.array; cell(hand.NumRows-NumRows,hand.NumCol)];
else
hand.NumRows = NumRows;
end
if (hand.NumRows < hand.MAX_ROWS), hand.MAX_ROWS = hand.NumRows; end
else % 'array' not transmited
hand.array = cell(hand.NumRows,hand.NumCol);
end
if (isempty(hand.ColNames) && ~isempty(hand.HdrButtons)) % By default columns are labeled 'A','B',...
hand.ColNames = cell(1,hand.NumCol);
for (i = 1:hand.NumCol), hand.ColNames{1,i} = char(i+64); end
end
if (size(hand.array,2) > size(hand.ColNames,2))
error('"ColNames" argument has less elements than the number of columns is "array"')
end
if (isempty(hand.ColWidth)) % Use default value for button width
hand.ColWidth = repmat(60,1,hand.NumCol);
elseif (numel(hand.ColWidth) == 1) % 'ColWidth' was a scalar
hand.ColWidth = repmat(hand.ColWidth,1,hand.NumCol);
end
if (~isempty(hand.RowNumbers)) % Row numbering was requested
hand.ColWidth = [35 hand.ColWidth];
hand.NumCol = hand.NumCol + 1;
hand.array = [cell(hand.NumRows,1) hand.array];
d_col = 1;
end
end
arr_pos_xi = hand.left_marg + [0 (cumsum(hand.ColWidth+hand.bd_size))];
arr_pos_xi(end) = []; % We don't want the last element
arr_pos_xw = hand.ColWidth;
% ---------------- Create the figure ----------------------------------
fig_height = min(hand.NumRows,hand.MAX_ROWS) * (hand.RowHeight+hand.bd_size);
if (~isempty(hand.HdrButtons)), fig_height = fig_height + 22; end % Make room for header buttons
if (~isempty(hand.modal)), fig_height = fig_height + 30; end % Make room for OK,Cancel buttons
pos = [5 75 sum(arr_pos_xw)+hand.left_marg+(hand.NumCol-1)*hand.bd_size+15 fig_height]; % The 15 is for the slider
nW = hand.RowNamesWidth; % Short name for the row names width buttons
if (~isempty(hand.checks)), pos(3) = pos(3) + 15; end % Account for checkboxes size
if (~isempty(hand.RowNames)), pos(3) = pos(3) + nW; end % Account for row names size
hand.hFig = figure('unit','pixels','NumberTitle','off','Menubar','none','resize','on','position', ...
pos,'Name',hand.FigName,'Resize','off','Visible','off');
movegui(hand.hFig,hand.position)
hand.arr_pos_y = (fig_height-hand.RowHeight-hand.bd_size - (0:hand.NumRows-1)*(hand.RowHeight+hand.bd_size))';
if (~isempty(hand.HdrButtons)), hand.arr_pos_y = hand.arr_pos_y - 22; end
if (~isempty(hand.checks) && isempty(hand.RowNames)) % Create the checkboxes uicontrols
arr_pos_xi = arr_pos_xi + 15; % Make room for them
hand.hChecks = zeros(hand.NumRows,1);
hand.Checks_pos_orig = [ones(hand.NumRows,1)*7 (hand.arr_pos_y+3) ones(hand.NumRows,1)*15 ones(hand.NumRows,1)*15];
end
if (~isempty(hand.RowNames) && isempty(hand.checks)) % Create the row names
arr_pos_xi = arr_pos_xi + nW; % Make room for them
hand.RowNames_pos_orig = [ones(hand.NumRows,1)*7 (hand.arr_pos_y) ones(hand.NumRows,1)*nW ones(hand.NumRows,1)*20];
end
if (~isempty(hand.checks) && ~isempty(hand.RowNames)) % Create both the checkboxes uicontrols and the row names
arr_pos_xi = arr_pos_xi + 15 + nW + 2; % Make room for them
hand.hChecks = zeros(hand.NumRows,1);
hand.Checks_pos_orig = [ones(hand.NumRows,1)*7 (hand.arr_pos_y+3) ones(hand.NumRows,1)*15 ones(hand.NumRows,1)*15];
hand.RowNames_pos_orig = [ones(hand.NumRows,1)*7 + 15 + 5 (hand.arr_pos_y) ones(hand.NumRows,1)*nW ones(hand.NumRows,1)*20];
end
hand.hEdits = zeros(hand.NumRows,hand.NumCol);
hand.Edits_pos_orig = cell(hand.NumRows,hand.NumCol);
% ---------------- Create the edit uicontrols ---------------------------
for (i = 1:hand.NumRows)
if (~isempty(hand.checks))
hand.hChecks(i) = uicontrol('Style','checkbox','unit','pixels','position', ...
hand.Checks_pos_orig(i,:),'Value',1);
end
for (j = 1:hand.NumCol)
hand.Edits_pos_orig{i,j} = [arr_pos_xi(j) hand.arr_pos_y(i) arr_pos_xw(j) 20];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -