function out = OverlayTableEdit(d,defCell)
%OverlayTableEdit Utility function to edit the contents of a table.
%
%OverlayTableEdit requires "GUI Layout Toolbox" written by Ben Tordoff
%which is available from the MATLAB File Exchange.
%
%Inputs
%======
% d ...................... Cell array of strings or scalars which serve
% as the default contents for editing.
%
%Outputs
% out .................... Updated cell array. If Cancel is pressed (or
% escape key or dialog close box selected) out
% will be returned as empty.
%
%See also: OverlayTable
% Jon Caspar
% $Date: 2012/07/31 20:04:55 $
% $Revision: 1.2 $
if ~ismember('defCell',who)
defCell = [1,1];
end
out = [];
myName = 'Edit Table Contents';
gui.modal = 'normal';
gui.fig = figure('IntegerHandle','off');
set(gui.fig,...
'Units','pixels',...
'Menubar','none',...
'Name',myName,...
'Number','off',...
'HandleVisibility','callback',...
'Visible','on',...
'Tag',myName,...
'WindowStyle',gui.modal);
gui.VB0 = uiextras.VBox(...
'Parent',gui.fig,...
'Units','normalized',...
'Position',[0,0,1,1],...
'Spacing',0,...
'Padding',20);
[nrows,ncols] = size(d);
r = zeros(nrows+1,1);
tb = nan(nrows,ncols);
for j=1:nrows
r(j) = uiextras.HBox('Parent',gui.VB0);
for k=1:ncols
tb(j,k) = uicontrol('Parent',double(r(j)),...
'Style','edit',...
'String',d{j,k},...
'KeyPressFcn',@UserKey,...
'BackgroundColor','w');
end
end
r(end+1) = uiextras.HBox('Parent',gui.VB0,...
'Padding',10,...
'Spacing',10);
uicontrol('Parent',r(end),...
'Style','pushbutton',...
'String','Cancel',...
'Callback',@bCancel);
uicontrol('Parent',r(end),...
'Style','pushbutton',...
'String','OK',...
'Callback',@bOK);
gui.VB0.Sizes = [ones(1,nrows)*30,-1];
p = get(gui.fig,'Position');
set(gui.fig,'Position',[p(1),p(2),p(3),nrows*30+100])
uicontrol(tb(defCell(1),defCell(2)));%focus to first cell
waitfor(gui.fig)
%===== Callbacks ==========================================================
function bCancel(~,~)
out = [];
uiresume(gui.fig);
set(gui.fig,'Visible','off')
drawnow
delete(gui.fig);
end
function bOK(~,~)
out = cell(nrows,ncols);
for jj = 1:nrows;
for kk=1:ncols
out{jj,kk} = get(tb(jj,kk),'String');
end
end
uiresume(gui.fig);
set(gui.fig,'Visible','off')
drawnow
delete(gui.fig);
end
function UserKey(hObject,ev)
switch ev.Key
case 'escape'
% User said cancel by hitting escape
bCancel()
case 'return'
try
if strcmp(get(hObject,'Style'),'edit')
drawnow;% don't ask... lose the active entry without the pause
end
catch
end
bOK()
case 'downarrow'
myhandle = get(gui.fig,'CurrentObject');
[jj,kk] = find(tb == myhandle);
jj = jj + 1;
if jj > size(tb,1)
jj = 1;
end
uicontrol(tb(jj,kk))
case 'uparrow'
myhandle = get(gui.fig,'CurrentObject');
[jj,kk] = find(tb == myhandle);
jj = jj - 1;
if jj < 1
jj = size(tb,1);
end
uicontrol(tb(jj,kk))
% case 'rightarrow'
% myhandle = get(gui.fig,'CurrentObject');
% [jj,kk] = find(tb == myhandle);
% kk = kk + 1;
% if kk > size(tb,2)
% kk = 1;
% end
% uicontrol(tb(jj,kk))
% case 'leftarrow'
% myhandle = get(gui.fig,'CurrentObject');
% [jj,kk] = find(tb == myhandle);
% kk = kk - 1;
% if kk < 1
% kk = size(tb,2);
% end
% uicontrol(tb(jj,kk))
case 'home'
uicontrol(tb(1,1))
case 'end'
uicontrol(tb(end,end))
otherwise
end
end
end