设计APP
主界面
函数方法
定时器
classdef MemoryMonitorAppExample < matlab. apps. AppBase% Properties that correspond to app componentsproperties ( Access = public) UIFigure matlab. ui. FigureStopButton matlab. ui. control. ButtonStartButton matlab. ui. control. ButtonSubtitle matlab. ui. control. LabelTitle matlab. ui. control. LabelUIAxes matlab. ui. control. UIAxesendproperties ( Access = private) RandTimer % Timer objectPlotLine % Line objectendmethods ( Access = private) function RandTimerFcn ( app, ~ , ~ ) % Generate a random numberrandnum = rand; % Update YData in plotydata = app. PlotLine. YData; ydata = circshift ( ydata, 1 ) ; ydata ( 1 ) = randnum; app. PlotLine. YData = ydata; end end% Callbacks that handle component eventsmethods ( Access = private) % Code that executes after component creationfunction startupFcn ( app) % Configure x- and y- axisapp. UIAxes. XLim = [ 0 60 ] ; app. UIAxes. XDir = "reverse" ; app. UIAxes. YLim = [ 0 1 ] ; % Initial plot is all zerosapp. PlotLine = plot ( app. UIAxes, 0 : 60 , zeros ( 1 , 61 ) ) ; % Create timer objectapp. 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 functionend% Button pushed function: StartButtonfunction StartButtonPushed ( app, event) % If timer is not running, start itif strcmp ( app. RandTimer. Running, "off" ) start ( app. RandTimer) ; endend% Button pushed function: StopButtonfunction StopButtonPushed ( app, event) % Stop the timerstop ( app. RandTimer) ; end% Close request function: UIFigurefunction UIFigureCloseRequest ( app, event) % Stop timer, then delete timer and appstop ( app. RandTimer) ; delete ( app. RandTimer) ; delete ( app) ; endend% Component initializationmethods ( Access = private) % Create UIFigure and componentsfunction createComponents ( app) % Create UIFigure and hide until all components are createdapp. 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 UIAxesapp. 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 Titleapp. 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 Subtitleapp. Subtitle = uilabel ( app. UIFigure) ; app. Subtitle. Position = [ 253 409 140 22 ] ; app. Subtitle. Text = 'Calculated Every Second' ; % Create StartButtonapp. StartButton = uibutton ( app. UIFigure, 'push' ) ; app. StartButton. ButtonPushedFcn = createCallbackFcn ( app, @StartButtonPushed, true) ; app. StartButton. Position = [ 197 59 100 22 ] ; app. StartButton. Text = 'Start' ; % Create StopButtonapp. 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 createdapp. UIFigure. Visible = 'on' ; endend% App creation and deletionmethods ( Access = public) % Construct appfunction app = MemoryMonitorAppExample% Create UIFigure and componentscreateComponents ( app) % Register the app with App DesignerregisterApp ( app, app. UIFigure) % Execute the startup functionrunStartupFcn ( app, @startupFcn) if nargout == 0 clear appendend% Code that executes before app deletionfunction delete ( app) % Delete UIFigure when app is deleteddelete ( app. UIFigure) endend
end