【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
热门推荐
精准控制隧道出入口灯光,消除隧道“黑洞”和“白洞”现象
秒懂!日本公司注册首选城市推荐
如何选择高品质的涂料产品?涂料产品的质量如何判断?
岭回归(概念+实例)
中国如果强制执行双休制度,会发生什么?结果可能颠覆你的认知!
除了影响健康,空气质量对马拉松成绩影响有多大?最新研究揭示....
参加节目时,李连杰曾高调宣传壹基金:它让我对得住邓公的重视!
文件夹禁止写入?教你简单有效的设置方法
Spring Cloud微服务链路跟踪Sleuth详解
尿酸高了,南瓜子还能“嗑”吗?
ChIP引物设计教程
引物设计数据库如何选择
如何正确配置Cloudflare的CDN服务?
实用干货!教你如何巧妙降低电流纹波
如何打造爆款文章?从选题到灵感,一篇教程全给你讲透了
“北极航道”开通对跨境电商的意义,不止于海运物流成本降低
【膳食纤维知识百科】膳食纤维的作用 高膳食纤维的食物有哪些
商品、价值与货币:市场经济的运行逻辑
北方栽种月季品种
看透一个免疫细胞:B cell
如何撰写'租金另行协商还款'的申请书及协商函?
民间租赁合同追责指南:从约定到法律规定
C语言如何输入int型
体脂率标准值参考表:不同体型的体脂率范围及健康建议
基于Spring Boot的中小学生错题管理系统设计与实现
健康科普:白内障手术的"时机密码",你解锁了吗?
二打一生肖是什么生肖?丹凤眼又对应哪个生肖?
国债期货与利率的关系
春钓鲫鱼时如何选择合适的钓具和饵料?有哪些技巧?
具身智能综述:鹏城实验室&中大调研近400篇文献,深度解析具身智能