一、初识APP Designer
整个界面主要包括包括左侧的组件库、中间的APP界面即UIFigure和右侧的组件浏览器,从左侧选择组件拖入UIFigure,组件浏览器显示当前已添加的各组件,可根据需要在浏览器修改组件名称变量,修改后的名称会自动更新到代码中。左上角的画图提供了对各组件的排列、对齐等辅助排版功能,右下角的组件属性检查器可看到所选中的组件的属性,例如文本、位置、大小、可视性等,可按需修改。
二、添加组件
从左侧组件库选择组件添加到界面上,可在右下的组件检查器中修改组件显示的文本,在组件浏览器中修改组件在代码中的名称。例如下图中的按钮,在APP界面上显示文本“导入数据”,在代码视图中用app.Button表示该按钮。右键可添加该按钮的回调函数,例如ButtonPushedFcn回调,即点击该按钮后将会执行的代码。
三、添加startupFcn函数
如有需要在APP启动时运行的程序,例如初始化、验证等,可通过添加startupFcn函数,在该函数内编写需要执行的代码,如没有该需求则忽略。
四、画图小工具案例
以设计一个画图的小工具为例,假设想要实现的功能包括导入xlsx文件,查看文件数据,并可选择变量进行作图,在APP启动前还需要进行信息确认等。
根据功能添加所需的组件,并调整组件布局,添加回调函数。界面没怎么调整,有点辣眼睛,勿怪~
界面视图:
完整代码:
classdef app < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ListBox matlab.ui.control.ListBox
ListBoxLabel matlab.ui.control.Label
Button_2 matlab.ui.control.Button
UITable matlab.ui.control.Table
DropDown matlab.ui.control.DropDown
DropDownLabel matlab.ui.control.Label
Button matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
InputData % Description
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
selection = uiconfirm(app.UIFigure,' 帅否 ?','身份验证',...
'Options',{'帅','否'},'DefaultOption',1);
switch selection
case '帅'
app.ListBox.Multiselect='on'; % 设置组件ListBox可多选
case '否'
delete(app)
otherwise
delete(app)
end
end
% Button pushed function: Button
function ButtonPushed(app, event)
% "导入数据"按钮
[file,path]=uigetfile('*.xlsx'); % 选择.xlsx文件
app.InputData=readtable(fullfile(path,file)); % 读取文件
app.UITable.ColumnName=app.InputData.Properties.VariableNames; % 设置UITable组件列标题
app.UITable.Data=app.InputData.Variables; % 赋值UITable数据
app.DropDown.Items=app.InputData.Properties.VariableNames; % 设置DropDown组件下拉选项
app.ListBox.Items=app.InputData.Properties.VariableNames; % 设置ListBox组件可选项
end
% Button pushed function: Button_2
function Button_2Pushed(app, event)
% "画图"按钮
x=table2array(app.InputData(:,app.DropDown.Value));
y=table2array(app.InputData(:,app.ListBox.Value));
plot(app.UIAxes,x,y)
legend(app.UIAxes,app.ListBox.Value)
grid(app.UIAxes,'on')
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 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [332 54 300 244];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [49 394 100 39];
app.Button.Text = '导入数据';
% Create DropDownLabel
app.DropDownLabel = uilabel(app.UIFigure);
app.DropDownLabel.HorizontalAlignment = 'right';
app.DropDownLabel.Position = [391 432 66 22];
app.DropDownLabel.Text = 'Drop Down';
% Create DropDown
app.DropDown = uidropdown(app.UIFigure);
app.DropDown.Position = [472 432 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.Position = [21 76 302 185];
% Create Button_2
app.Button_2 = uibutton(app.UIFigure, 'push');
app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);
app.Button_2.Position = [49 334 100 36];
app.Button_2.Text = '画图';
% Create ListBoxLabel
app.ListBoxLabel = uilabel(app.UIFigure);
app.ListBoxLabel.HorizontalAlignment = 'right';
app.ListBoxLabel.Position = [400 371 48 22];
app.ListBoxLabel.Text = 'List Box';
% Create ListBox
app.ListBox = uilistbox(app.UIFigure);
app.ListBox.Position = [463 321 100 74];
% 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 = app
% 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