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 =[060];
app.UIAxes.XDir ="reverse";
app.UIAxes.YLim =[01];% 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
ifstrcmp(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 =[100100640480];
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 =[53100508300];% Create Title
app.Title =uilabel(app.UIFigure);
app.Title.HorizontalAlignment ='center';
app.Title.FontSize =16;
app.Title.Position =[26743010822];
app.Title.Text ='Output of rand';% Create Subtitle
app.Subtitle =uilabel(app.UIFigure);
app.Subtitle.Position =[25340914022];
app.Subtitle.Text ='Calculated Every Second';% Create StartButton
app.StartButton =uibutton(app.UIFigure,'push');
app.StartButton.ButtonPushedFcn =createCallbackFcn(app, @StartButtonPushed, true);
app.StartButton.Position =[1975910022];
app.StartButton.Text ='Start';% Create StopButton
app.StopButton =uibutton(app.UIFigure,'push');
app.StopButton.ButtonPushedFcn =createCallbackFcn(app, @StopButtonPushed, true);
app.StopButton.Position =[3485910022];
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