0

Is there a way to delete a row in uitable using the mouse right click, similar to the way done in excel? I am looking to do so in order to save just the data I want and not all the table.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
user1212200
  • 353
  • 1
  • 7
  • 15

1 Answers1

1

You could introduce a pushbutton:

function myTable 

close all
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);

tb = uitoolbar(h);
uipushtool(tb,'ClickedCallback',@deleteRow);
end

function cellSelect(src,evt)
index = evt.Indices;
if any(index)
    rows = index(:,1);
    set(src,'UserData',rows);
end
end

function deleteRow(~,~)
th = findobj('Tag','myTable');
data = get(th,'Data');
rows = get(th,'UserData');
mask = (1:size(data,1))';
mask(rows) = [];
data = data(mask,:);
set(th,'Data',data);
end

for example:

enter image description here

Select a row and push the button to delete it. You can also select multiple rows and delete them at once!

It's not exactly what you wanted, but your request seems quite hard to realize. You will need to deal with java objects, which is not the most trivial way to go.

I don't want to say it's impossible, but you'll save a lot of time if you just go with the pushbutton. Matlab is just not made for that :)

In this example the button has no icon, to add one, read this article

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113