Machine Learning Algorithm – Polynomial Regression in MATLAB App Designer R2018a

This page shows the steps to design an App Designer to implement one of the Machine Learning Algorithm, Polynomial Regression, using App Designer in MATLAB R2018a.

For implementation it uses Polyfit and polyval commands.

The Interface design:

MachineLearning_App_Designer

The complete code of the above video is:

classdef MachineLearningAlgorithm_PolynomialRegression < matlab.apps.AppBase

% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
ImportDataButton matlab.ui.control.Button
PolynomialDeterminationButton matlab.ui.control.Button
AccuracyOrderSpinnerLabel matlab.ui.control.Label
AccuracyOrderSpinner matlab.ui.control.Spinner
ClearAxesButton matlab.ui.control.Button
PredictFutureRangePercentageKnobLabel matlab.ui.control.Label
PredictFutureRangePercentageKnob matlab.ui.control.Knob
end
properties (Access = private)
Data; % Description
Range; % Description
PolyNom; % Description
Predicted_Data; % Description
end
% Callbacks that handle component events
methods (Access = private)

% Button pushed function: ImportDataButton
function ImportDataButtonPushed(app, event)
app.Data = xlsread(‘SENSEX.csv’);
app.Data = app.Data(:,4);
plot(app.UIAxes, app.Data);
hold(app.UIAxes);

end

% Button pushed function: PolynomialDeterminationButton
function PolynomialDeterminationButtonPushed(app, event)
app.Range = 1:length(app.Data);
app.Range = app.Range’;
app.PolyNom = polyfit(app.Range, app.Data, app.AccuracyOrderSpinner.Value);

app.Predicted_Data = polyval(app.PolyNom, app.Range);
plot(app.UIAxes, app.Predicted_Data);

end

% Button pushed function: ClearAxesButton
function ClearAxesButtonPushed(app, event)
cla(app.UIAxes);
end

% Value changed function: PredictFutureRangePercentageKnob
function PredictFutureRangePercentageKnobValueChanged(app, event)
value = app.PredictFutureRangePercentageKnob.Value;

Future_Data = polyval(app.PolyNom, 1:app.Range(end)*(1+value/100));

plot(app.UIAxes,Future_Data);

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 UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, ‘Title’)
xlabel(app.UIAxes, ‘X’)
ylabel(app.UIAxes, ‘Y’)
app.UIAxes.Position = [32 191 577 263];

% Create ImportDataButton
app.ImportDataButton = uibutton(app.UIFigure, ‘push’);
app.ImportDataButton.ButtonPushedFcn = createCallbackFcn(app, @ImportDataButtonPushed, true);
app.ImportDataButton.Position = [32 49 100 22];
app.ImportDataButton.Text = ‘Import Data’;

% Create PolynomialDeterminationButton
app.PolynomialDeterminationButton = uibutton(app.UIFigure, ‘push’);
app.PolynomialDeterminationButton.ButtonPushedFcn = createCallbackFcn(app, @PolynomialDeterminationButtonPushed, true);
app.PolynomialDeterminationButton.Position = [155 49 152 22];
app.PolynomialDeterminationButton.Text = ‘Polynomial Determination’;

% Create AccuracyOrderSpinnerLabel
app.AccuracyOrderSpinnerLabel = uilabel(app.UIFigure);
app.AccuracyOrderSpinnerLabel.HorizontalAlignment = ‘right’;
app.AccuracyOrderSpinnerLabel.Position = [43 125 96 22];
app.AccuracyOrderSpinnerLabel.Text = ‘Accuracy – Order’;

% Create AccuracyOrderSpinner
app.AccuracyOrderSpinner = uispinner(app.UIFigure);
app.AccuracyOrderSpinner.Limits = [1 20];
app.AccuracyOrderSpinner.ValueDisplayFormat = ‘%.0f’;
app.AccuracyOrderSpinner.HorizontalAlignment = ‘center’;
app.AccuracyOrderSpinner.Position = [154 125 100 22];
app.AccuracyOrderSpinner.Value = 5;

% Create ClearAxesButton
app.ClearAxesButton = uibutton(app.UIFigure, ‘push’);
app.ClearAxesButton.ButtonPushedFcn = createCallbackFcn(app, @ClearAxesButtonPushed, true);
app.ClearAxesButton.Position = [509 445 100 22];
app.ClearAxesButton.Text = ‘Clear Axes’;

% Create PredictFutureRangePercentageKnobLabel
app.PredictFutureRangePercentageKnobLabel = uilabel(app.UIFigure);
app.PredictFutureRangePercentageKnobLabel.HorizontalAlignment = ‘center’;
app.PredictFutureRangePercentageKnobLabel.Position = [425 40 196 22];
app.PredictFutureRangePercentageKnobLabel.Text = ‘Predict – Future Range Percentage’;

% Create PredictFutureRangePercentageKnob
app.PredictFutureRangePercentageKnob = uiknob(app.UIFigure, ‘continuous’);
app.PredictFutureRangePercentageKnob.Limits = [0 10];
app.PredictFutureRangePercentageKnob.MajorTicks = [0 2 4 6 8 10];
app.PredictFutureRangePercentageKnob.ValueChangedFcn = createCallbackFcn(app, @PredictFutureRangePercentageKnobValueChanged, true);
app.PredictFutureRangePercentageKnob.MinorTicks = [];
app.PredictFutureRangePercentageKnob.Position = [492 96 60 60];

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

% 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

Posts/ Comments from YouTube channel:

  • Link to the data set used in the video:

Historical data from the below URL is used : https://www.bseindia.com/indices/IndexArchiveData.aspx

There are many other websites which provide this data. Feel free to use any data. The concept shown in the video should work with various data points.

Leave a Reply

%d bloggers like this: