【问题标题】:Set limits for values in the cells of an uitable为 uitable 的单元格中的值设置限制
【发布时间】:2020-07-08 15:14:28
【问题描述】:

我正在使用 Matlab 的 App Designer (2019b) 创建一个 GUI。 NumericEditField 的一个很好的功能是您可以定义值限制,以便用户无法输入超出所需范围的值。例如,以下内容会将编辑字段值限制在 -100 到 100 之间。

app.numericEditField1.Limits = [-100 100];

我的 GUI 中还有一个 uitable 对象 - 是否可以为数据表中的单元格设置值限制,例如编辑字段?我没有看到明显等价的属性。我最好的解决方法是编辑 CellEditCallback 以在每次更改值时手动检查值。

下面是一个示例应用程序,它有一个带限制的值编辑字段和一个常规的uitable。我也想对表格的某些列设置值限制。

示例代码

classdef sampleLimitedValApp < matlab.apps.AppBase

% Properties that correspond to app components
properties (Access = public)
    UIFigure                        matlab.ui.Figure
    LimitedEditValueEditFieldLabel  matlab.ui.control.Label
    LimitedEditValueEditField       matlab.ui.control.NumericEditField
    UITable                         matlab.ui.control.Table
end

% Callbacks that handle component events
methods (Access = private)

    % Code that executes after component creation
    function startupFcn(app)
        app.UITable.Data = zeros(3,4);
    end
end

% Component initialization
methods (Access = private)

    % Create UIFigure and components
    function createComponents(app)

        % Create UIFigure and hide until all components are created
        app.UIFigure = uifigure('Visible', 'off');
        app.UIFigure.Position = [100 100 383 331];
        app.UIFigure.Name = 'UI Figure';

        % Create LimitedEditValueEditFieldLabel
        app.LimitedEditValueEditFieldLabel = uilabel(app.UIFigure);
        app.LimitedEditValueEditFieldLabel.HorizontalAlignment = 'right';
        app.LimitedEditValueEditFieldLabel.Position = [31 280 101 22];
        app.LimitedEditValueEditFieldLabel.Text = 'Limited Edit Value';

        % Create LimitedEditValueEditField
        app.LimitedEditValueEditField = uieditfield(app.UIFigure, 'numeric');
        app.LimitedEditValueEditField.Limits = [-100 100];
        app.LimitedEditValueEditField.Position = [147 280 100 22];

        % Create UITable
        app.UITable = uitable(app.UIFigure);
        app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
        app.UITable.RowName = {''};
        app.UITable.ColumnEditable = true;
        app.UITable.Position = [31 67 302 185];

        % Show the figure after all components are created
        app.UIFigure.Visible = 'on';
    end
end

% App creation and deletion
methods (Access = public)

    % Construct app
    function app = sampleLimitedValApp

        % Create UIFigure and components
        createComponents(app)

        % Register the app with App Designer
        registerApp(app, app.UIFigure)

        % Execute the startup function
        runStartupFcn(app, @startupFcn)

        if nargout == 0
            clear app
        end
    end

    % Code that executes before app deletion
    function delete(app)

        % Delete UIFigure when app is deleted
        delete(app.UIFigure)
    end
end
end

【问题讨论】:

  • 请提供一个最小的工作示例。
  • @thewaywewalk 我添加了一些代码。当我什至不知道是否可行时,我不确定哪种工作示例合适。
  • @thewaywewalk 感谢您的参考。我将一个快速示例与一个具有我想要的功能的值字段以及我想要应用到的 uitable 放在一起。
  • 这是可以使用的东西 ;)

标签: matlab matlab-figure matlab-uitable matlab-app-designer


【解决方案1】:

您使用CellEditCallback 的想法是正确的。我不得不承认,我并不是真正的在 Matlab 中创建和使用类的专家,并且总是在不使用 AppDesigner 的情况下从头开始创建我的 GUI,这就是为什么我不知道是否可能有更好的函数和方法组织.

但是,以下是您想要的:

% Component initialization
methods (Access = private)

    % Create UIFigure and components
    function createComponents(app)

        % original code

        % added code
        app.UITable.CellEditCallback = @limitCellVal;
        function limitCellVal(src,evt)
            CellLimits = [-100 100];
            idx = evt.Indices; % indices of selected cell
            belowLowerLimit = src.Data(idx(1),idx(2)) < CellLimits(1);
            aboveUpperLimit = src.Data(idx(1),idx(2)) > CellLimits(2);
            if belowLowerLimit, src.Data(idx(1),idx(2)) = CellLimits(1); end
            if aboveUpperLimit, src.Data(idx(1),idx(2)) = CellLimits(2); end
        end

    end

end

如果您想一次编辑多个单元格,则需要稍微调整回调函数,但我认为您可以做到。

【讨论】:

  • 太好了,这正是我想要的。谢谢你一直陪着我!
猜你喜欢
  • 2013-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
  • 2021-03-10
  • 1970-01-01
  • 2016-01-18
相关资源
最近更新 更多