为uitable单元格中的值设置限制



我正在使用Matlab的应用程序设计器(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

您使用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

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

最新更新