How to design a simple counter using App Designer private functions and properties?

This video shows the use of private and public functions and properties of the matlab class in the app designer. This video demonstrate the steps to build a simple counter using internal functions and properties of the App Designer class.

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source code:

classdef Conuter_AppDesigner_Func_Properties < matlab.apps.AppBase

% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
StartButton matlab.ui.control.Button
Label matlab.ui.control.Label
TotalCountLabel matlab.ui.control.Label
end
properties (Access = private)
TotalCount = 0; % Variable to store the counted value
end

methods (Access = private)

function results = increment(app)
results = app.TotalCount + 1;
end

function results = decrement(app)
results = app.TotalCount – 1;
end

end
% Callbacks that handle component events
methods (Access = private)

% Button pushed function: StartButton
function StartButtonPushed(app, event)

for i = 1:20
switch randi(2)
case 1
% Increment
app.TotalCount = app.increment();

case 2
% Decrement
app.TotalCount = app.decrement();
end
app.Label.Text = num2str(app.TotalCount);
pause(1);
end

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 = ‘UI Figure’;

% Create StartButton
app.StartButton = uibutton(app.UIFigure, ‘push’);
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
app.StartButton.Position = [302 126 100 22];
app.StartButton.Text = ‘Start’;

% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.HorizontalAlignment = ‘center’;
app.Label.FontSize = 28;
app.Label.Position = [270 263 163 71];
app.Label.Text = ‘0’;

% Create TotalCountLabel
app.TotalCountLabel = uilabel(app.UIFigure);
app.TotalCountLabel.VerticalAlignment = ‘top’;
app.TotalCountLabel.FontSize = 24;
app.TotalCountLabel.Position = [85 303 127 31];
app.TotalCountLabel.Text = ‘Total Count’;

% 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 = Conuter_AppDesigner_Func_Properties

% Create UIFigure and components
createComponents(app)

% Register the app with App Designer
registerApp(app, app.UIFigure)

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

Leave a Reply

%d bloggers like this: