通过创建辅助函数,您可以使常用命令具有单一源代码,避免维护冗余代码。
私有函数通常在单窗口 App 中使用,而公共函数通常在多窗口 App 中使用。
1 创建辅助函数
- 点击编辑器选项卡中函数按钮的向下箭头展开下拉菜单
- 选择代码浏览器中的函数选项卡,选择私有函数或公共函数。
管理辅助函数
- 更改辅助函数名称时,App 设计工具会自动更新对该函数的所有引用。
官方示例

set_param % 设置系统和模块参数值
classdef sf_mediaplayer_strings_ui < matlab.apps.AppBase
% 对应于应用程序组件的属性
properties (Access = public)
UIFigure matlab.ui.Figure
AlbumNameEditFieldLabel matlab.ui.control.Label
AlbumNameEditField matlab.ui.control.EditField
RadioRequestPanel matlab.ui.container.Panel
OFFButton matlab.ui.control.Button
AMButton matlab.ui.control.Button
FMButton matlab.ui.control.Button
CDButton matlab.ui.control.Button
CDRequestPanel matlab.ui.container.Panel
STOPButton matlab.ui.control.Button
PLAYButton matlab.ui.control.Button
REWButton matlab.ui.control.Button
FFButton matlab.ui.control.Button
MediaPlayerHelperLabel matlab.ui.control.Label
InsertDiscButton matlab.ui.control.Button
EjectDiscButton matlab.ui.control.Button
end
methods (Access = private)
% 值更改函数:AlbumNameEditField
function AlbumNameEditFieldValueChanged(app, event)
set_param('sf_mediaplayer_strings/Album','String','"'+string(app.AlbumNameEditField.Value)+'"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% 按钮按下功能:关闭按钮
function OFFButtonPushed(app, event)
set_param('sf_mediaplayer_strings/RR','String','"OFF"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: AMButton
function AMButtonPushed(app, event)
set_param('sf_mediaplayer_strings/RR','String','"AM"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: FMButton
function FMButtonPushed(app, event)
set_param('sf_mediaplayer_strings/RR','String','"FM"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: CDButton
function CDButtonPushed(app, event)
set_param('sf_mediaplayer_strings/RR','String','"CD"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
set_param('sf_mediaplayer_strings/CR','String','"STOP"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: PLAYButton
function PLAYButtonPushed(app, event)
set_param('sf_mediaplayer_strings/CR','String','"PLAY"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: REWButton
function REWButtonPushed(app, event)
set_param('sf_mediaplayer_strings/CR','String','"REW"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: FFButton
function FFButtonPushed(app, event)
set_param('sf_mediaplayer_strings/CR','String','"FF"');
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: InsertDiscButton
function InsertDiscButtonPushed(app, event)
set_param('sf_mediaplayer_strings/ID','Value','1');
set_param('sf_mediaplayer_strings/ED','Value','0');
end
% Button pushed function: EjectDiscButton
function EjectDiscButtonPushed(app, event)
set_param('sf_mediaplayer_strings/ID','Value','0');
set_param('sf_mediaplayer_strings/ED','Value','1');
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
set_param('sf_mediaplayer_strings/SS','Value','1');
delete(app);
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 639 439];
app.UIFigure.Name = 'UI Figure';
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create AlbumNameEditFieldLabel
app.AlbumNameEditFieldLabel = uilabel(app.UIFigure);
app.AlbumNameEditFieldLabel.HorizontalAlignment = 'right';
app.AlbumNameEditFieldLabel.VerticalAlignment = 'top';
app.AlbumNameEditFieldLabel.Position = [66 321 76 15];
app.AlbumNameEditFieldLabel.Text = 'Album Name';
% Create AlbumNameEditField
app.AlbumNameEditField = uieditfield(app.UIFigure, 'text');
app.AlbumNameEditField.ValueChangedFcn = createCallbackFcn(app, @AlbumNameEditFieldValueChanged, true);
app.AlbumNameEditField.HorizontalAlignment = 'center';
app.AlbumNameEditField.Position = [157 317 418 22];
app.AlbumNameEditField.Value = 'Handel''s Greatest Hits';
% Create RadioRequestPanel
app.RadioRequestPanel = uipanel(app.UIFigure);
app.RadioRequestPanel.Title = 'Radio Request';
app.RadioRequestPanel.Position = [66 147 509 79];
% Create OFFButton
app.OFFButton = uibutton(app.RadioRequestPanel, 'push');
app.OFFButton.ButtonPushedFcn = createCallbackFcn(app, @OFFButtonPushed, true);
app.OFFButton.Position = [46 13 69 28];
app.OFFButton.Text = 'OFF';
% Create AMButton
app.AMButton = uibutton(app.RadioRequestPanel, 'push');
app.AMButton.ButtonPushedFcn = createCallbackFcn(app, @AMButtonPushed, true);
app.AMButton.Position = [213 13 69 28];
app.AMButton.Text = 'AM';
% Create FMButton
app.FMButton = uibutton(app.RadioRequestPanel, 'push');
app.FMButton.ButtonPushedFcn = createCallbackFcn(app, @FMButtonPushed, true);
app.FMButton.Position = [304 13 69 28];
app.FMButton.Text = 'FM';
% Create CDButton
app.CDButton = uibutton(app.RadioRequestPanel, 'push');
app.CDButton.ButtonPushedFcn = createCallbackFcn(app, @CDButtonPushed, true);
app.CDButton.Position = [396 13 69 28];
app.CDButton.Text = 'CD';
% Create CDRequestPanel
app.CDRequestPanel = uipanel(app.UIFigure);
app.CDRequestPanel.Title = 'CD Request';
app.CDRequestPanel.Position = [67 38 509 79];
% Create STOPButton
app.STOPButton = uibutton(app.CDRequestPanel, 'push');
app.STOPButton.ButtonPushedFcn = createCallbackFcn(app, @STOPButtonPushed, true);
app.STOPButton.Position = [46 10 69 28];
app.STOPButton.Text = 'STOP';
% Create PLAYButton
app.PLAYButton = uibutton(app.CDRequestPanel, 'push');
app.PLAYButton.ButtonPushedFcn = createCallbackFcn(app, @PLAYButtonPushed, true);
app.PLAYButton.Position = [213 10 69 28];
app.PLAYButton.Text = 'PLAY';
% Create REWButton
app.REWButton = uibutton(app.CDRequestPanel, 'push');
app.REWButton.ButtonPushedFcn = createCallbackFcn(app, @REWButtonPushed, true);
app.REWButton.Position = [303 10 69 28];
app.REWButton.Text = 'REW';
% Create FFButton
app.FFButton = uibutton(app.CDRequestPanel, 'push');
app.FFButton.ButtonPushedFcn = createCallbackFcn(app, @FFButtonPushed, true);
app.FFButton.Position = [396 10 69 28];
app.FFButton.Text = 'FF';
% Create MediaPlayerHelperLabel
app.MediaPlayerHelperLabel = uilabel(app.UIFigure);
app.MediaPlayerHelperLabel.VerticalAlignment = 'top';
app.MediaPlayerHelperLabel.FontSize = 16;
app.MediaPlayerHelperLabel.FontWeight = 'bold';
app.MediaPlayerHelperLabel.Position = [242 379 158 20];
app.MediaPlayerHelperLabel.Text = 'Media Player Helper';
% Create InsertDiscButton
app.InsertDiscButton = uibutton(app.UIFigure, 'push');
app.InsertDiscButton.ButtonPushedFcn = createCallbackFcn(app, @InsertDiscButtonPushed, true);
app.InsertDiscButton.Position = [180 261 100 22];
app.InsertDiscButton.Text = 'Insert Disc';
% Create EjectDiscButton
app.EjectDiscButton = uibutton(app.UIFigure, 'push');
app.EjectDiscButton.ButtonPushedFcn = createCallbackFcn(app, @EjectDiscButtonPushed, true);
app.EjectDiscButton.Position = [363 261 100 22];
app.EjectDiscButton.Text = 'Eject Disc';
end
end
methods (Access = public)
% Construct app
function app = sf_mediaplayer_strings_ui
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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
学习使我进步 看完点个赞呗 ─=≡Σ(((つ•̀ω•́)つ
网友评论