Matlab 为uitable的单元格中的值设置限制
我正在使用Matlab的应用程序设计器(2019b)创建GUI。Matlab 为uitable的单元格中的值设置限制,matlab,matlab-figure,matlab-uitable,matlab-app-designer,Matlab,Matlab Figure,Matlab Uitable,Matlab App Designer,我正在使用Matlab的应用程序设计器(2019b)创建GUI。NumericEditField的一个很好的特性是,您可以定义值限制,这样用户就不能输入超出所需范围的值。例如,以下内容将编辑字段值限制在-100和100之间 app.numericEditField1.Limits = [-100 100]; 我的GUI中还有一个uitable对象-是否可以像编辑字段一样为数据表中的单元格设置值限制?我没有看到一个明显等价的财产。我的最佳解决方法是编辑CellEditCallback,以便在每次
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
classdef sampleLimitedValApp
您使用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
%组件初始化
方法(访问=私有)
%创建图形和组件
函数createComponents(应用程序)
%原始代码
%添加代码
app.UITable.CellEditCallback=@limitCellVal;
函数limitCellVal(src、evt)
上限=[-100];
idx=evt.指数;%选定单元格的索引
belowLowerLimit=src.Data(idx(1),idx(2))单元限制(2);
如果低于下限,则src.Data(idx(1),idx(2))=单元极限(1);结束
如果高于上限,则src.数据(idx(1),idx(2))=单元限值(2);结束
结束
结束
结束
如果您想一次编辑多个单元格,则需要稍微调整回调函数,但我认为您可以做到。请提供一个简单的工作示例。@thewaywewalk我添加了一些代码。当我甚至不知道某件事是否可行时,我不确定哪种工作示例是合适的。@thewaywewalk感谢您的参考。我创建了一个快速示例,其中包含一个具有所需功能的值字段,以及一个我希望应用该功能的uitable。这是一个需要使用的工具;)太好了,这正是我想要的。谢谢你一直支持我!