Clear Filters
Clear Filters

How to implement right-click and double click in matlab uitables?

92 views (last 30 days)
I am making a gui with a table of elements, that I want to access and control using the mouse. For example, I want to be able to highlight/select items, double click on items and right click items, much like how I can interact with files and folders in finder or windows explorer.
The CellSelectionCallback is great, because it gives access to which cell is selected. However, it seems I cannot implement a doubleclick action since the CellSelectionCallback does not trigger if a cell is already selected, and the ButtonDownFcn does not capture left mouseclicks any more.
I can also implement a uipopupmenu, but whenever I rightclick a cell, 1) that cell does not get highlighted (which I find very non-intuitive) and 2) I dont have access to which cell was selected (unless I get the mouse cursor position, scrollbar position etc. to figure out which cell was clicked upon).
Am I missing an obvious way to implement what I want? Is there any reason why these kinds of mouse operations (capturing double clicks and right clicks) on specific cells, which are so common place in both file browsers and spreadsheet software are not easy to implement in uitables?
  1 Comment
Sven on 11 Nov 2019
Upvoted for visibility. Here's some minimal working example code.
function uitableCellSelectionExample()
f = uifigure;
myTab = table([1;2],["Click";"or DblClick"]);
function onCellSelect(srcTab,event)
uifigH = srcTab.Parent;
uifigH.SelectionType % This is *always* "normal" even on double-click
rand(1) % Because re-clicking a selected cell doesn't trigger a new event

Sign in to comment.

Accepted Answer

Yuval Cohen
Yuval Cohen on 5 Apr 2021
The issue is that the CellSelectionCallback is triggered on the first, and only the first click and the callback function will not yet know if it is a single or double click.
The simple workaround I found is to add the following code to the callback function:
function uitableCellSelectionExample()
fig_handle = uifigure;
data = num2cell(magic(5));
uitable(fig_handle,'Data',data,'CellSelectionCallback',{@cell_click, fig_handle}) % pass the figure handle as an argument
% ----------------------------------------------------------------------
function cell_click(table_handle, event, fig_handle)
pause(0.5); % to allow the user time to add a second click
if strcmpi(fig_handle.SelectionType, 'open')
fprintf(1, 'Double click on cell %d, %d\n', event.Indices(1,1), event.Indices(1,2));
fprintf(1, 'Single click on cell %d, %d\n', event.Indices(1,1), event.Indices(1,2));

More Answers (0)


Find more on Migrate GUIDE Apps in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!