【MATLAB】App 设计 (入门)
创作时间:
作者:
@小白创作中心
【MATLAB】App 设计 (入门)
引用
CSDN
1.
https://m.blog.csdn.net/qq_36666115/article/details/137830815
本文将介绍如何使用MATLAB创建一个简单的随机数生成器应用程序。通过本教程,你将学习到如何使用MATLAB的App Designer创建一个包含定时器功能的GUI应用程序。
设计APP
主界面
函数方法
定时器
classdef MemoryMonitorAppExample < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
StopButton matlab.ui.control.Button
StartButton matlab.ui.control.Button
Subtitle matlab.ui.control.Label
Title matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
RandTimer % Timer object
PlotLine % Line object
end
methods (Access = private)
function RandTimerFcn(app,~,~)
% Generate a random number
randnum = rand;
% Update YData in plot
ydata = app.PlotLine.YData;
ydata = circshift(ydata,1);
ydata(1) = randnum;
app.PlotLine.YData = ydata;
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
% Configure x- and y- axis
app.UIAxes.XLim = [0 60];
app.UIAxes.XDir = "reverse";
app.UIAxes.YLim = [0 1];
% Initial plot is all zeros
app.PlotLine = plot(app.UIAxes,0:60,zeros(1,61));
% Create timer object
app.RandTimer = timer(...
"ExecutionMode", "fixedRate", ... % Run timer repeatedly
"Period", 1, ... % Period is 1 second
"BusyMode", "queue",... % Queue timer callbacks when busy
"TimerFcn", @app.RandTimerFcn); % Specify callback function
end
% Button pushed function: StartButton
function StartButtonPushed(app, event)
% If timer is not running, start it
if strcmp(app.RandTimer.Running, "off")
start(app.RandTimer);
end
end
% Button pushed function: StopButton
function StopButtonPushed(app, event)
% Stop the timer
stop(app.RandTimer);
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
% Stop timer, then delete timer and app
stop(app.RandTimer);
delete(app.RandTimer);
delete(app);
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 = 'Random Number Generator';
app.UIFigure.Resize = 'off';
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
xlabel(app.UIAxes, 'Seconds')
ylabel(app.UIAxes, 'Random number calculated')
app.UIAxes.XTickLabelRotation = 0;
app.UIAxes.YTickLabelRotation = 0;
app.UIAxes.ZTickLabelRotation = 0;
app.UIAxes.Box = 'on';
app.UIAxes.XGrid = 'on';
app.UIAxes.YGrid = 'on';
app.UIAxes.Position = [53 100 508 300];
% Create Title
app.Title = uilabel(app.UIFigure);
app.Title.HorizontalAlignment = 'center';
app.Title.FontSize = 16;
app.Title.Position = [267 430 108 22];
app.Title.Text = 'Output of rand';
% Create Subtitle
app.Subtitle = uilabel(app.UIFigure);
app.Subtitle.Position = [253 409 140 22];
app.Subtitle.Text = 'Calculated Every Second';
% Create StartButton
app.StartButton = uibutton(app.UIFigure, 'push');
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
app.StartButton.Position = [197 59 100 22];
app.StartButton.Text = 'Start';
% Create StopButton
app.StopButton = uibutton(app.UIFigure, 'push');
app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
app.StopButton.Position = [348 59 100 22];
app.StopButton.Text = 'Stop';
% 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 = MemoryMonitorAppExample
% 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
热门推荐
秋冬饮茶指南:日照红茶的正确打开方式
股票市场的三大风险及规避方法
像巴菲特一样克服心理障碍,做理性投资者
沪深300指数八连涨!彭博社:或成市场重要里程碑
班主任如何做好学生心理工作?这份指南请收好
优秀的班主任都有一个共同点:为学生提供情绪价值!
窦唯:国摇辉煌的缔造者,前半生在滚圈封神,后半生步入凡尘
芯片(集成电路)是谁发明的?
晶体管:科技创新的核心驱动力
时光的回声:托马斯·爱迪生与留声机的发明
水痘疫苗副作用知多少?专家揭秘!
宝宝打水痘疫苗后高烧怎么办?
上海城隍庙:八百年古刹的文化传承与现代新生
科普|千万不要把鼻炎当感冒!儿童过敏性鼻炎有这些症状
两岁宝宝鼻炎检查方法全解析
张振宇的情歌,你最爱哪一首?
张振宇《不要再来伤害我》:一首歌,一个故事
画风抄袭风波再起:知识产权如何保护艺术家?
画师必读:如何避免画风抄袭风波
安迪·沃霍尔的“抄袭”争议:艺术的边界在哪?
真实历史上的佐罗原型:一位反美斗士的英勇事迹
全国助残日:科技助残助力18岁聋哑人开启人生新篇
剑桥大学双学位申请攻略:从准备到录取
2025最新韶山旅游门票价格,韶山故居门票多少钱?老人和小孩门票优惠政策
圣三一&哥大双学位:语言提升秘籍
ChatGPT教你用魅力评价赢得人心
儿童过敏性鼻炎怎么办?做好预防,远离过敏原
OKR制定指南:原则与案例分析
长征出发90周年!5分钟梳理长征路上的重大决策和重要会议
《一起China Fun》:李宇春易烊千玺的春晚新作引发热议