首先您需要将单元格可编辑性设置为默认为false:
set(hTable,'ColumnEditable', [false false ...]); %accordingly your number of columns
并介绍一个CellSelectionCallback:
set(hTable,'CellSelectionCallback',@cellSelect);
在同一脚本中调用以下函数
function cellSelect(src,evt)
getstate = get(src,'ColumnEditable'); %gets status of editability
index = evt.Indices; %index of clicked cell
state = [false false ...]; %set all cells to default: not editable
state(index) = ~getstate(index); %except the clicked one, was it
%already false before set it true
set(src,'ColumnEditable', state) %pass property to table
end
还有一个CellEditCallback:
set(hTable,'CellEditCallback',@cellEdit);
打电话
function cellEdit(src,~)
state = [false false ...];
set(src,'ColumnEditable', state)
end
小例子
function minimalTable
h = figure('Position',[600 400 402 100],'numbertitle','off','MenuBar','none');
defaultData = {'insert number...' , 'insert number...'};
uitable(h,'Units','normalized','Position',[0 0 1 1],...
'Data', defaultData,...
'ColumnName', [],'RowName',[],...
'ColumnWidth', {200 200},...
'ColumnEditable', [false false],...
'ColumnFormat', {'numeric' , 'numeric'},...
'CellSelectionCallback',@cellSelect);
end
function cellSelect(src,evt)
getstate = get(src,'ColumnEditable');
index = evt.Indices;
state = [false false];
state(index) = ~getstate(index);
set(src,'ColumnEditable', state)
end
function cellEdit(src,~)
state = [false false];
set(src,'ColumnEditable', state)
end
正如您所发现的,这并不总是有效。因为您遇到的问题与我之前在弹出菜单方面遇到的问题相同。这是完全相同的问题:ColumnEditable 只是一个行向量而不是矩阵。我不得不处理ColumnFormat 属性,它也只是一个行向量。如果双击功能对你来说真的很重要,可以参考以下两个答案:
Is it possible to prevent an uitable popup menu from popping up? Or: How to get a callback by clicking a cell, returning the row & column index?
How to deselect cells in uitable / how to disable cell selection highlighting?
线程基本上建议为每一行创建一个唯一的uitable,以便每一行都有一个唯一的ColumnEditable 属性。这是迄今为止唯一的解决方案。
恐怕没有简单的解决方案。除了其他答案的复杂解决方法外,我无法为您提供进一步的帮助。或者只是使用上面的简单方法并忍受一些小缺点。