1

我正在使用 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
4

1 回答 1

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

如果您想一次编辑多个单元格,则需要对回调函数进行一些调整,但我认为您可以做到这一点。

于 2020-03-31T11:47:36.230 回答