1 访问和更新回调中的 UI 组件内的数据
app.Component.Property
2 获取并设置一个仪表的 Value 属性
x = app.PressureGauge.Value; % Get the gauge value
app.PressureGauge.Value = 50; % Set the gauge value to 50
3 共享某个中间结果或多个回调需要访问的数据
- 应定义公共或私有属性来存储数据
公共属性在 App 内部和外部均可访问,而私有属性只能在 App 内部访问。 - 描述该属性
properties (Access = public)
X % Average cost
end
- 代码需要在 App 启动时访问某个属性值
在 properties 块或在 StartupFcn 回调中对其值进行初始化
% 初始化
properties (Access = public)
X = 5; % Average cost
end
% 在代码的其他位置,使用圆点表示法获取或设置属性的值
y = app.X % Get the value of X
app.X = 5; % Set the value of X
public,protected,private权限
Access=private:表示只有该类的成员方法可以访问该数据,而子类和其他外部函数无法访问该成员变量。
Access=protected:表示只有该类的成员方法还有该类的子类而已访问该数据。
public:在类的定义中、类的成员方法、子类的成员是方法都可以访问这个成员变量,类之外的函数或者脚本也可以访问这个成员变量。
作者:hainingwyx
链接:https://www.jianshu.com/p/87d5d0634a2d
5 官方示例
共享绘图数据和下拉列表选择classdef ConfigurePlotAppExample < matlab.apps.AppBase
% 对应于应用程序组件的属性
properties (Access = public)
ConfigurePlotUIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
SampleSizeEditFieldLabel matlab.ui.control.Label
SampleSizeEditField matlab.ui.control.NumericEditField
ColormapDropDownLabel matlab.ui.control.Label
ColormapDropDown matlab.ui.control.DropDown
UpdatePlotButton matlab.ui.control.Button
end
%% 共享数据传递 %%%%%%%%%%
properties (Access = private)
Z = peaks(35); % 要绘制的曲面数据
end
%%%%%%%%%%%%%%%%%%%%
methods (Access = private)
function plotsurface(app)
% Plot Z
surf(app.UIAxes,app.Z);
% 设置颜色映射
cmap = app.ColormapDropDown.Value;
colormap(app.UIAxes,cmap);
end
end
methods (Access = private)
% 组件创建后执行的代码
function startupFcn(app)
plotsurface(app);
end
% 按钮按下功能:UpdatePlotButton
function UpdatePlotButtonPushed(app, event)
plotsurface(app);
end
% 值更改函数:SampleSizeEditField
function SampleSizeEditFieldValueChanged(app, event)
sampleSize = app.SampleSizeEditField.Value;
% Update the Z property
app.Z = peaks(sampleSize);
end
end
% 应用程序初始化和构造
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create ConfigurePlotUIFigure
app.ConfigurePlotUIFigure = uifigure;
app.ConfigurePlotUIFigure.Position = [100 100 495 365];
app.ConfigurePlotUIFigure.Name = 'Configure Plot';
% Create UIAxes
app.UIAxes = uiaxes(app.ConfigurePlotUIFigure);
app.UIAxes.Position = [150 32 323 287];
% Create SampleSizeEditFieldLabel
app.SampleSizeEditFieldLabel = uilabel(app.ConfigurePlotUIFigure);
app.SampleSizeEditFieldLabel.HorizontalAlignment = 'right';
app.SampleSizeEditFieldLabel.VerticalAlignment = 'top';
app.SampleSizeEditFieldLabel.Position = [37 257 74 15];
app.SampleSizeEditFieldLabel.Text = 'Sample Size';
% Create SampleSizeEditField
app.SampleSizeEditField = uieditfield(app.ConfigurePlotUIFigure, 'numeric');
app.SampleSizeEditField.Limits = [2 1000];
app.SampleSizeEditField.ValueChangedFcn = createCallbackFcn(app, @SampleSizeEditFieldValueChanged, true);
app.SampleSizeEditField.Position = [24 230 100 22];
app.SampleSizeEditField.Value = 35;
% Create ColormapDropDownLabel
app.ColormapDropDownLabel = uilabel(app.ConfigurePlotUIFigure);
app.ColormapDropDownLabel.HorizontalAlignment = 'right';
app.ColormapDropDownLabel.VerticalAlignment = 'top';
app.ColormapDropDownLabel.Position = [44 202 59 15];
app.ColormapDropDownLabel.Text = 'Colormap';
% Create ColormapDropDown
app.ColormapDropDown = uidropdown(app.ConfigurePlotUIFigure);
app.ColormapDropDown.Items = {'Parula', 'Jet', 'Winter', 'Cool'};
app.ColormapDropDown.Position = [24 175 100 22];
app.ColormapDropDown.Value = 'Parula';
% Create UpdatePlotButton
app.UpdatePlotButton = uibutton(app.ConfigurePlotUIFigure, 'push');
app.UpdatePlotButton.ButtonPushedFcn = createCallbackFcn(app, @UpdatePlotButtonPushed, true);
app.UpdatePlotButton.Position = [24 122 100 22];
app.UpdatePlotButton.Text = 'Update Plot';
end
end
methods (Access = public)
% 构建应用程序
function app = ConfigurePlotAppExample
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.ConfigurePlotUIFigure)
% 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.ConfigurePlotUIFigure)
end
end
end
学习使我进步 看完点个赞呗 ─=≡Σ(((つ•̀ω•́)つ
网友评论