【问题标题】:Delete row in a UItable upon selection using right mouse button - Matlab 2020a使用鼠标右键删除选择表中的行 - Matlab 2020a
【发布时间】:2020-06-19 15:32:05
【问题描述】:

在此功能(应用程序设计器)中,我在使用鼠标左键选择时删除表格中的一行,如何使用右键或其他键+鼠标组合而不是左键进行选择?

代码:

% 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 

后面的代码:

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

【问题讨论】:

    标签: matlab matlab-app-designer


    【解决方案1】:

    可以通过父级uifigure(我称之为app.figMain)检测到选择uitable 时按下的任何修饰键(即shift、control)。修饰符通过未记录的命令modifiers = get(app.figMain,'CurrentModifier') 在元胞数组中返回。您可以使用if 语句,该语句仅在modifiers 中未列出按键时运行。换句话说,表格选择回调只针对左键运行,而不针对 shift-click、ctrl-click 等。

    在给定的代码中实现它:

        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 描述了CurrentModifier 命令:CurrentModifier

    【讨论】:

    • 我在脚本中添加了figSelect.SelectionType ='alt';,但它不起作用。知道为什么吗?
    • SelectionType 返回一个字符串,所以== 比较运算符不起作用(= 运算符只设置值,不能比较它们)。您需要使用strcmp 来比较字符串。见strcmp。此外,单独添加该行并没有任何用处。仅当单击鼠标右键时才使用if strcmp(figSelect.SelectionType,'alt') 后跟您的代码运行代码。
    • 我不想比较。我的愿望是使用鼠标右键运行selection,因此我需要像使用figSelect.SelectionType ='alt'; 一样设置它,但它不起作用。也许您可以添加 if 命令,以便我可以关注
    • 我在上面更新了我的答案。 figSelect.SelectionType 返回鼠标点击的类型;它无法设置图形响应的点击类型。
    • 非常感谢您的帮助,但这并不能回答我的问题。顺便说一句,uifigure 声明在循环内。
    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2021-03-31
    • 2012-02-02
    • 2017-09-14
    • 1970-01-01
    • 2015-09-02
    • 2014-12-19
    相关资源
    最近更新 更多