2

我想删除 uitable 的选定行。我考虑过为每一行使用一个复选框来选择我要删除的那个。

但是我不能将复选框设置为 false 的列添加到表中。

我得到这个跟随错误

??? Conversion to cell from logical is not possible.

Error in ==> loadTable at 7
    data(:,5) = true;

我试过这个:

 function loadTable(hTable, arrayHeaderAndData, columnFormatAtt)

        header = arrayHeaderAndData{1};
        % Add column delete
        header = [header 'Del'];
        data = arrayHeaderAndData{2};
        data(:,5) = true;
        columnFormatCases = [columnFormatCases 'logical'];

        set(hTable, 'Data',data,...
            'visible','on',...
            'ColumnWidth','auto',...
            'ColumnName',header,...
            'ColumnEditable', [false false false false],...
            'ColumnFormat', columnFormatAtt
    );


    end

然后我需要从表中删除所有带有选中复选框的行。我怎样才能做到这一点?

4

2 回答 2

1

不需要带有复选框的另一列,只是为了指示您要删除哪一行。我宁愿使用uipushtool来添加一个删除按钮,它会删除以前选择的所有行。

function myTable 

h = figure('Position',[600 400 402 100],'numbertitle','off','MenuBar','none');

defaultData = rand(5,2);
uitable(h,'Units','normalized','Position',[0 0 1 1],...
              'Data', defaultData,... 
              'Tag','myTable',...    
              'ColumnName', [],'RowName',[],...
              'CellSelectionCallback',@cellSelect);

% create pushbutton to delete selected rows
tb = uitoolbar(h);
uipushtool(tb,'ClickedCallback',@deleteRow);
end

function cellSelect(src,evt)
% get indices of selected rows and make them available for other callbacks
index = evt.Indices;
if any(index)             %loop necessary to surpress unimportant errors.
    rows = index(:,1);
    set(src,'UserData',rows);
end
end

function deleteRow(~,~)
th = findobj('Tag','myTable');
% get current data
data = get(th,'Data');
% get indices of selected rows
rows = get(th,'UserData');
% create mask containing rows to keep
mask = (1:size(data,1))';
mask(rows) = [];
% delete selected rows and re-write data
data = data(mask,:);
set(th,'Data',data);
end
于 2013-11-05T16:15:48.853 回答
1

似乎您正在尝试分配给一个单元格,而不是分配给一个单元格。

这是我会尝试的:

data(:,5) = {true}

或者:

[data{:,5}] = deal(true)

如果你还在苦苦读help cell

于 2013-11-05T14:58:01.240 回答