【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
热门推荐
选择理想床垫的关键因素:硬度、材质与个人偏好
貔貅的外形特征及其辨别方法 如何识别真正的貔貅
专家提醒:定期进行甲状腺功能检测 为健康预警
怎样降低期货交易中的风险因素?
乌龟临死前的征兆有哪些
陕西历史博物馆预约不上就去秦汉馆,地铁直达,展馆面积大更好玩
12种降低低密度胆固醇的食物
个人和公司档案存档所需材料及流程详解
希腊神话、北欧神话、罗马神话和埃及神话的区别
固态电池产业前景大揭秘!这些关键技术突破正在加速落地
如何书写一份正式的请假申请书
有氧运动:健身爱好者的指南
产城观察丨面向物流运输的成都低空经济应用场景简析
三分钟读懂云计算相关知识
什么是集合的幂集?
苏轼三首写雨佳作,气势磅礴,雄伟壮观,写出了他豪迈奔放的气概
国内金价偏高的原因是什么?国内金价受哪些因素影响?
SSD硬盘恢复出厂设置:全面指南
茶叶中的茶多酚:抗氧化、抗衰老、消炎杀菌等保健功能
基于机器学习的二手房房价数据分析与价格预测模型
脊柱疼痛的原因是什么?
客户体验提升之数据驱动与关键指标解析
美国探亲签证申请的流程有哪些
寒冬腊月,这些四字成语描绘了最美的冬日景象!
HTML弹窗示例
私募和公募的区别在哪里?这些区别对投资者有怎样的影响?
CTPA对急性肺栓塞的预后评估作用
PPT动画制作指南:提升演示文稿吸引力的实用技巧
涨知识:“六亲不认”中的六亲,究竟指的是哪六亲?
宝宝怎么转奶?隔顿转奶法具体如何操作?