【MATLAB】App 设计 (入门)
设计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
原文地址:https://blog.csdn.net/qq_36666115/article/details/137830815
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!