如何删除合适的行?

时间:2013-11-05 14:50:24

标签: matlab matlab-figure matlab-guide matlab-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

然后我需要删除表中所选复选框的所有行。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

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

以下是我要尝试的内容:

data(:,5) = {true}

或者替代:

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

如果您仍在努力阅读help cell

答案 1 :(得分: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