3

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);
AsiJapan
  • 313
  • 1
  • 10

1 Answers1

-1

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

Ben Farris
  • 19
  • 5
  • I added `figSelect.SelectionType ='alt';` in the script but it does not work. any idea why? – AsiJapan Jun 22 '20 at 06:28
  • `SelectionType` returns a string, so the `==` comparison operator doesn't work (The `=` operator only sets values and can't compare them). You need to use `strcmp` to compare strings. See [strcmp](https://www.mathworks.com/help/matlab/ref/strcmp.html). Also, adding the line by itself doesn't do anything useful. Use `if strcmp(figSelect.SelectionType,'alt')` followed by your code to run the code only if the right-mouse button was clicked. – Ben Farris Jun 22 '20 at 14:21
  • I do not wish to compare. My desire is to use the right mouse button to run the `selection` and therefore I need to set it as I did with `figSelect.SelectionType ='alt'; ` but it does not work. Maybe you can add the if command so I can follow – AsiJapan Jun 22 '20 at 15:34
  • 1
    I updated my answer above. `figSelect.SelectionType` _returns_ what type of mouse click was used; it cannot _set_ what type of click the figure responds to. – Ben Farris Jun 22 '20 at 19:12
  • Thank a lot for the help but this does not answer my question. BTW the `uifigure` declaration is inside the loop. – AsiJapan Jun 22 '20 at 19:39
  • I somewhat misunderstood your original question (sorry). It turns out that `SelectionType` only registers when blank regions of the figure are clicked. There is a different command (see above) that checks if the shift key is currently pressed when the table selection callback runs. This sorts out left-click from shift+left-click. – Ben Farris Jun 23 '20 at 00:39
  • How do I get my equivalent to your `app.figMain`? – AsiJapan Jun 23 '20 at 16:44
  • Try `app.infoTable.Parent` or the name of the figure in App Designer that holds the uitable (the default is probably `app.UIFigure`). – Ben Farris Jun 23 '20 at 17:11
  • Thanks for the persistence. i changed to app.infoTable.Parent and I get this error `Error using isempty Too many input arguments.` – AsiJapan Jun 23 '20 at 17:31
  • I fixed a type in the third line of code. Now it reads `if isempty(modifiers)`. – Ben Farris Jun 23 '20 at 23:04
  • It does not work. I think Mathworks will need to have in house solution for this issue – AsiJapan Jun 25 '20 at 07:38