Delete row in a UItable upon selection using right mouse button - Matlab 2020a

Viewed 524

In this function (app designer) I delete a row in a table upon selection using the left button of the mouse, how can I use the right button or another key+mouse combination for selection instead of the left?

Code:

% Cell selection callback: infoTable
        function infoTableCellSelection(app, event)
            indices = event.Indices;
            data = app.infoTable.Data;
            row = indices(:,1);
            figSelect = uifigure;
            figSelect.SelectionType ='alt'; 
            selection = uiconfirm(figSelect,'Delete row?','Confirm Close',...
                        'Icon','question');
            disp(selection); 
            disp(figSelect.SelectionType);
            switch selection
            case 'OK'
                app.infoTable.Data(row,:) = [];
                close(figSelect);
            case 'Cancel'
                close(figSelect);
                return
            end 

Later in the code:

function createComponents(app)
...
    app.infoTable.CellSelectionCallback = createCallbackFcn(app, @infoTableCellSelection, true);
1 Answers

Any modifiers keys (i.e. shift, control) that are being pressed when the uitable is selected can be detected through the parent uifigure (I'll call it app.figMain). The modifiers are returned in a cell array via the undocumented command modifiers = get(app.figMain,'CurrentModifier'). You can use an if statement that only runs if no key presses are listed in modifiers. In other words, the table selection callback only runs for a left-click, but not for shift-click, ctrl-click, etc.

Implementing this in the given code:

    function infoTableCellSelection(app, event)
        modifiers = get(app.figMain,'CurrentModifier');
        if isempty(modifiers) % Check that shift-key, ctrl-key, etc. is not pressed
            indices = event.Indices;
            data = app.infoTable.Data;
            row = indices(:,1);
            figSelect = uifigure;
            selection = uiconfirm(figSelect,'Delete row?','Confirm Close',...
                        'Icon','question');
            disp(selection); 
            disp(figSelect.SelectionType);
            switch selection
            case 'OK'
                app.infoTable.Data(row,:) = [];
                close(figSelect);
            case 'Cancel'
                close(figSelect);
                return
            end
        end
    end 

MATLAB Undocumented describes the CurrentModifier command: CurrentModifier

Related