matlab - 表格单元格只能通过双击进行编辑

标签 matlab matlab-uitable

只需单击即可编辑表格单元格,我希望仅通过双击即可编辑它。只需单击即可选择单元格。

我正在使用 uitable 的这个属性:

set(hTable, 'Data',data,...
    'ColumnEditable', edit,...

最佳答案

首先,您需要将单元格可编辑性默认设置为 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 属性。这是迄今为止唯一的解决方案。

恐怕没有简单的解决方案。除了其他答案的复杂解决方法之外,我无法为您提供进一步的帮助。或者只使用上面的简单方法并接受一些小缺点。

关于matlab - 表格单元格只能通过双击进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19789747/

相关文章:

matlab - 是否可以防止弹出合适的弹出菜单?或者 : How to get a callback by clicking a cell, 返回行和列索引?

math - 如何评估matlab中函数的导数?

java - 在 Matlab 中限制 uitable 中的有效数字。从Java导入的数据

matlab - 以独特的方式平铺向量 MATLAB

arrays - 逻辑数组 - 在赋值 A(I) = B 中,B 和 I 中的元素数必须相同

matlab - 在 Matlab uitable 中设置不同的单元格格式

matlab - uimenu 按钮保持按下状态,只需在其上滑动即可触发其他菜单 : pushbutton behaviour desired

matlab - 索引移动极差

algorithm - 如何找到覆盖另一个列表中所有元素所需的最小列表数