【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
热门推荐
吃香蕉可以止泻吗?探討香蕉在腸道健康中的角色
系统管理员admin密码安全设置指南
IT设备硬件升级指南:如何选择合适的升级方案
夫妻离婚户口本可以分开吗
联合国:2024年全球发生了超150场“史无前例”的气候灾难
目前女孩子学啥技能好?
第六篇 可逆氢电极RHE及其应用
谷胱甘肽为什么是备受瞩目的化妆品成分?
大雁为什么要离群?
出国留学回国就业趋势和前景分析
南江黄羊,从秦巴山区走出的"明星羊"
蒸鸡蛋放油好还是不放油好?一文详解口感与营养的平衡之道
如何降低内存频率?一文教你调节内存频率的图文操作方法
小区成了“停车场”?物业只能“尽力劝说”
到底什么是矿山机器人,有文件可以参考了!
正确评估及处理儿童发热
家用NAS选购指南:千元预算打造高性价比家庭云存储,避坑攻略全解析
胃疼怎么缓解疼痛
日本研究团队揭示氟化物固体电解质离子传导机制
MMScan数据集:首个最大的多模态3D场景数据集,包含层次化的语言标注
逻辑思维混乱怎么训练?九大方法助你提升逻辑思维能力
耳机为什么没声音
英防空反导体系建设道阻且长
非全日制研究生的未来趋势:政策红利与就业前景
古代皇子在长到成年之后 他们都会被封为王吗
Windows最基本和常用的25个快捷键,建议收藏
小六壬入门基础知识:从起源到实战的完整指南
肩周炎粘连怎么治疗最好最有效
Camera Link 接口、usb接口、GigE 接口的对比
猪肝番茄汤的制作方法及营养价值(健康营养,一锅滋补的家常汤)