MIMO Neural Network in Matlab App Designer

1 view (last 30 days)
Xiao Colin
Xiao Colin on 5 Jun 2020
Commented: Hasan Falah on 10 Apr 2021
Hi.
I created a multiple input and multiple output neural network in Neural Network Toolbox to numerical predication.
Then I would like to put this neural network into Matlab App Designer , to allow other users to input their personal information(eg. age and BMI and...) and get their prediction physical exercise reference plan.
A simple application which have five inputs numeric edit fields, a predication botton and three outputs numeric edit fields.
My question is:
1.How to load my neural network into Matlab App Designer?
2.How do I match each input/output numeric edit fields with my neural network input/output variable?
The full code is the following one:
classdef ceshi < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ageLabel matlab.ui.control.Label
age matlab.ui.control.NumericEditField
BMIEditFieldLabel matlab.ui.control.Label
BMI matlab.ui.control.NumericEditField
VO2maxEditFieldLabel matlab.ui.control.Label
VO2max matlab.ui.control.NumericEditField
improvementLabel matlab.ui.control.Label
inprovement matlab.ui.control.NumericEditField
genderLabel matlab.ui.control.Label
gender matlab.ui.control.NumericEditField
botton matlab.ui.control.Button
intensityLabel matlab.ui.control.Label
intensity matlab.ui.control.NumericEditField
timeLabel matlab.ui.control.Label
time matlab.ui.control.NumericEditField
periodLabel matlab.ui.control.Label
period matlab.ui.control.NumericEditField
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
net=load('out1.mat');
app.net=app.net.net;
app.age.Value=0;
app.BMI.Value=0;
app.VO2max.Value=0;
app.inprovement.Value=0;
app.gender.Value=0;
app.intensity.Value=0;
app.time.Value=0;
app.period.Value=0;
end
% Button pushed function: botton
function bottonButtonPushed(app, event)
% Calculate the monthly payment赋值
years=app.age.Value;
bmi=app.BMI.Value;
vo2max=app.VO2max.Value;
gaishan=app.inprovement.Value;
gender=app.gender.Value;
X=[years;bmi;vo2max;gaishan;gender];
function [Y,Xf,Af] = out1(X,~,~)
% Map Minimum and Maximum Input Processing Function
function y = mapminmax_apply(x,settings)
y = bsxfun(@minus,x,settings.xoffset);
y = bsxfun(@times,y,settings.gain);
y = bsxfun(@plus,y,settings.ymin);
end
% Sigmoid Symmetric Transfer Function
function a = tansig_apply(n,~)
a = 2 ./ (1 + exp(-2*n)) - 1;
end
% Map Minimum and Maximum Output Reverse-Processing Function
function x = mapminmax_reverse(y,settings)
x = bsxfun(@minus,y,settings.ymin);
x = bsxfun(@rdivide,x,settings.gain);
x = bsxfun(@plus,x,settings.xoffset);
end
% Input 1
x1_step1.xoffset = [45;14.9;10.4;-0.01;0];
x1_step1.gain = [0.0425531914893617;0.0896860986547085;0.051948051948052;9.52380952380952;2];
x1_step1.ymin = -1;
% Layer 1
b1 = [0.2077630056204836928;0.29906504000866068749;1.3856748987931497563;1.1957978250273275656;-2.1580494625422255162;0.70550442411951030941;-1.6728612700070044639;0.82947313310971115818;-0.59046299676425550995;2.5755143917263927378];
IW1_1 = [-1.3078311393837158683 -0.076815014061756864416 1.2252829728281386945 -0.50859185266958384375 0.024743688724663669143;1.8808439460129384724 -0.13510147648511400265 -1.3337662266661434174 -5.094842594298493843 0.41450886466246755768;1.5215564974754081096 0.2911460061103159358 -2.4894724218515005276 2.2882676022263881421 0.29264788801240848581;0.31627397033031484641 -1.3472891939838265163 2.1670369182690296661 3.1601530845088880994 -0.04760288474124880348;-2.1232417660291438288 -0.34874738975597274759 3.9263605289755121319 -4.6855209982575223293 -0.36521205114056681795;-2.5088959162036581496 -0.35058568010602719767 2.7359800760507102524 -1.3173503992789636907 -0.03455841453851173084;-3.0383643930046764225 -1.8654135071099797738 0.70524059907392144808 2.8089427436700429297 -0.018315472627776559211;-3.8720128057227167773 0.46361696576009842063 3.6171251971096656241 -11.12221130095187327 -0.20093292819390778936;1.0414656091227529888 0.45473049737779436263 -2.5363219650633728186 -4.7590981882310163797 0.32484608851207774638;5.7158743473906898913 -5.8217925397695342582 -2.0053146799753358565 -0.44904764870374408448 -1.3245031868918870632];
% Layer 2
b2 = -2.1611136673690016785;
LW2_1 = [4.4423972988883102531 -2.2573987805113389093 4.5102750909969433479 3.1972793007625335271 2.7208419611510779035 -2.6050372476923002729 -0.54858233115846144479 1.0310766377749229772 3.8924816407254785311 0.31373065246327935807];
% Output 1
y1_step1.ymin = -1;
y1_step1.gain = 0.05;
y1_step1.xoffset = 50;
% ===== SIMULATION ========
% Format Input Arguments
isCellX = iscell(X);
if ~isCellX
X = {X};
end
% Dimensions
TS = size(X,2); % timesteps
if ~isempty(X)
Q = size(X{1},1); % samples/series
else
Q = 0;
end
% Allocate Outputs
Y = cell(1,TS);
% Time loop
for ts=1:TS
% Input 1
X{1,ts} = X{1,ts}';
Xp1 = mapminmax_apply(X{1,ts},x1_step1);
% Layer 1
a1 = tansig_apply(repmat(b1,1,Q) + IW1_1*Xp1);
% Layer 2
a2 = repmat(b2,1,Q) + LW2_1*a1;
% Output 1
Y{1,ts} = mapminmax_reverse(a2,y1_step1);
Y{1,ts} = Y{1,ts}';
end
% Final Delay States
Xf = cell(1,0);
Af = cell(2,0);
% Format Output Arguments
if ~isCellX
Y = cell2mat(Y);
end
Y=app.botton.Value;
app.botton.Value=Y;
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.Colormap = [0.2431 0.149 0.6588;0.2431 0.1529 0.6745;0.2471 0.1569 0.6863;0.2471 0.1608 0.698;0.251 0.1647 0.7059;0.251 0.1686 0.7176;0.2549 0.1725 0.7294;0.2549 0.1765 0.7412;0.2588 0.1804 0.749;0.2588 0.1843 0.7608;0.2627 0.1922 0.7843;0.2627 0.1961 0.7922;0.2667 0.2 0.8039;0.2667 0.2039 0.8157;0.2706 0.2078 0.8235;0.2706 0.2157 0.8353;0.2706 0.2196 0.8431;0.2745 0.2235 0.851;0.2745 0.2275 0.8627;0.2745 0.2314 0.8706;0.2745 0.2392 0.8784;0.2784 0.2431 0.8824;0.2784 0.2471 0.8902;0.2784 0.2549 0.898;0.2784 0.2588 0.902;0.2784 0.2667 0.9098;0.2784 0.2706 0.9137;0.2784 0.2745 0.9216;0.2824 0.2824 0.9255;0.2824 0.2863 0.9294;0.2824 0.2941 0.9333;0.2824 0.298 0.9412;0.2824 0.3059 0.9451;0.2824 0.3098 0.949;0.2824 0.3137 0.9529;0.2824 0.3216 0.9569;0.2824 0.3255 0.9608;0.2824 0.3294 0.9647;0.2784 0.3373 0.9686;0.2784 0.3412 0.9686;0.2784 0.349 0.9725;0.2784 0.3529 0.9765;0.2784 0.3569 0.9804;0.2784 0.3647 0.9804;0.2745 0.3686 0.9843;0.2745 0.3765 0.9843;0.2745 0.3804 0.9882;0.2706 0.3843 0.9882;0.2706 0.3922 0.9922;0.2667 0.3961 0.9922;0.2627 0.4039 0.9922;0.2627 0.4078 0.9961;0.2588 0.4157 0.9961;0.2549 0.4196 0.9961;0.251 0.4275 0.9961;0.2471 0.4314 1;0.2431 0.4392 1;0.2353 0.4431 1;0.2314 0.451 1;0.2235 0.4549 1;0.2196 0.4627 0.9961;0.2118 0.4667 0.9961;0.2078 0.4745 0.9922;0.2 0.4784 0.9922;0.1961 0.4863 0.9882;0.1922 0.4902 0.9882;0.1882 0.498 0.9843;0.1843 0.502 0.9804;0.1843 0.5098 0.9804;0.1804 0.5137 0.9765;0.1804 0.5176 0.9725;0.1804 0.5255 0.9725;0.1804 0.5294 0.9686;0.1765 0.5333 0.9647;0.1765 0.5412 0.9608;0.1765 0.5451 0.9569;0.1765 0.549 0.9529;0.1765 0.5569 0.949;0.1725 0.5608 0.9451;0.1725 0.5647 0.9412;0.1686 0.5686 0.9373;0.1647 0.5765 0.9333;0.1608 0.5804 0.9294;0.1569 0.5843 0.9255;0.1529 0.5922 0.9216;0.1529 0.5961 0.9176;0.149 0.6 0.9137;0.149 0.6039 0.9098;0.1451 0.6078 0.9098;0.1451 0.6118 0.9059;0.1412 0.6196 0.902;0.1412 0.6235 0.898;0.1373 0.6275 0.898;0.1373 0.6314 0.8941;0.1333 0.6353 0.8941;0.1294 0.6392 0.8902;0.1255 0.6471 0.8902;0.1216 0.651 0.8863;0.1176 0.6549 0.8824;0.1137 0.6588 0.8824;0.1137 0.6627 0.8784;0.1098 0.6667 0.8745;0.1059 0.6706 0.8706;0.102 0.6745 0.8667;0.098 0.6784 0.8627;0.0902 0.6824 0.8549;0.0863 0.6863 0.851;0.0784 0.6902 0.8471;0.0706 0.6941 0.8392;0.0627 0.698 0.8353;0.0549 0.702 0.8314;0.0431 0.702 0.8235;0.0314 0.7059 0.8196;0.0235 0.7098 0.8118;0.0157 0.7137 0.8078;0.0078 0.7176 0.8;0.0039 0.7176 0.7922;0 0.7216 0.7882;0 0.7255 0.7804;0 0.7294 0.7765;0.0039 0.7294 0.7686;0.0078 0.7333 0.7608;0.0157 0.7333 0.7569;0.0235 0.7373 0.749;0.0353 0.7412 0.7412;0.051 0.7412 0.7373;0.0627 0.7451 0.7294;0.0784 0.7451 0.7216;0.0902 0.749 0.7137;0.102 0.7529 0.7098;0.1137 0.7529 0.702;0.1255 0.7569 0.6941;0.1373 0.7569 0.6863;0.1451 0.7608 0.6824;0.1529 0.7608 0.6745;0.1608 0.7647 0.6667;0.1686 0.7647 0.6588;0.1725 0.7686 0.651;0.1804 0.7686 0.6471;0.1843 0.7725 0.6392;0.1922 0.7725 0.6314;0.1961 0.7765 0.6235;0.2 0.7804 0.6157;0.2078 0.7804 0.6078;0.2118 0.7843 0.6;0.2196 0.7843 0.5882;0.2235 0.7882 0.5804;0.2314 0.7882 0.5725;0.2392 0.7922 0.5647;0.251 0.7922 0.5529;0.2588 0.7922 0.5451;0.2706 0.7961 0.5373;0.2824 0.7961 0.5255;0.2941 0.7961 0.5176;0.3059 0.8 0.5059;0.3176 0.8 0.498;0.3294 0.8 0.4863;0.3412 0.8 0.4784;0.3529 0.8 0.4667;0.3686 0.8039 0.4549;0.3804 0.8039 0.4471;0.3922 0.8039 0.4353;0.4039 0.8039 0.4235;0.4196 0.8039 0.4118;0.4314 0.8039 0.4;0.4471 0.8039 0.3922;0.4627 0.8 0.3804;0.4745 0.8 0.3686;0.4902 0.8 0.3569;0.5059 0.8 0.349;0.5176 0.8 0.3373;0.5333 0.7961 0.3255;0.5451 0.7961 0.3176;0.5608 0.7961 0.3059;0.5765 0.7922 0.2941;0.5882 0.7922 0.2824;0.6039 0.7882 0.2745;0.6157 0.7882 0.2627;0.6314 0.7843 0.251;0.6431 0.7843 0.2431;0.6549 0.7804 0.2314;0.6706 0.7804 0.2235;0.6824 0.7765 0.2157;0.698 0.7765 0.2078;0.7098 0.7725 0.2;0.7216 0.7686 0.1922;0.7333 0.7686 0.1843;0.7451 0.7647 0.1765;0.7608 0.7647 0.1725;0.7725 0.7608 0.1647;0.7843 0.7569 0.1608;0.7961 0.7569 0.1569;0.8078 0.7529 0.1529;0.8157 0.749 0.1529;0.8275 0.749 0.1529;0.8392 0.7451 0.1529;0.851 0.7451 0.1569;0.8588 0.7412 0.1569;0.8706 0.7373 0.1608;0.8824 0.7373 0.1647;0.8902 0.7373 0.1686;0.902 0.7333 0.1765;0.9098 0.7333 0.1804;0.9176 0.7294 0.1882;0.9255 0.7294 0.1961;0.9373 0.7294 0.2078;0.9451 0.7294 0.2157;0.9529 0.7294 0.2235;0.9608 0.7294 0.2314;0.9686 0.7294 0.2392;0.9765 0.7294 0.2431;0.9843 0.7333 0.2431;0.9882 0.7373 0.2431;0.9961 0.7412 0.2392;0.9961 0.7451 0.2353;0.9961 0.7529 0.2314;0.9961 0.7569 0.2275;0.9961 0.7608 0.2235;0.9961 0.7686 0.2196;0.9961 0.7725 0.2157;0.9961 0.7804 0.2078;0.9961 0.7843 0.2039;0.9961 0.7922 0.2;0.9922 0.7961 0.1961;0.9922 0.8039 0.1922;0.9922 0.8078 0.1922;0.9882 0.8157 0.1882;0.9843 0.8235 0.1843;0.9843 0.8275 0.1804;0.9804 0.8353 0.1804;0.9765 0.8392 0.1765;0.9765 0.8471 0.1725;0.9725 0.851 0.1686;0.9686 0.8588 0.1647;0.9686 0.8667 0.1647;0.9647 0.8706 0.1608;0.9647 0.8784 0.1569;0.9608 0.8824 0.1569;0.9608 0.8902 0.1529;0.9608 0.898 0.149;0.9608 0.902 0.149;0.9608 0.9098 0.1451;0.9608 0.9137 0.1412;0.9608 0.9216 0.1373;0.9608 0.9255 0.1333;0.9608 0.9333 0.1294;0.9647 0.9373 0.1255;0.9647 0.9451 0.1216;0.9647 0.949 0.1176;0.9686 0.9569 0.1098;0.9686 0.9608 0.1059;0.9725 0.9686 0.102;0.9725 0.9725 0.0941;0.9765 0.9765 0.0863;0.9765 0.9843 0.0824];
app.UIFigure.Position = [100 100 627 280];
app.UIFigure.Name = 'MATLAB App';
% Create ageLabel
app.ageLabel = uilabel(app.UIFigure);
app.ageLabel.HorizontalAlignment = 'right';
app.ageLabel.Position = [77 216 26 22];
app.ageLabel.Text = 'age';
% Create age
app.age = uieditfield(app.UIFigure, 'numeric');
app.age.Position = [118 216 100 22];
% Create BMIEditFieldLabel
app.BMIEditFieldLabel = uilabel(app.UIFigure);
app.BMIEditFieldLabel.HorizontalAlignment = 'right';
app.BMIEditFieldLabel.Position = [76 175 27 22];
app.BMIEditFieldLabel.Text = 'BMI';
% Create BMI
app.BMI = uieditfield(app.UIFigure, 'numeric');
app.BMI.Position = [118 175 100 22];
% Create VO2maxEditFieldLabel
app.VO2maxEditFieldLabel = uilabel(app.UIFigure);
app.VO2maxEditFieldLabel.HorizontalAlignment = 'right';
app.VO2maxEditFieldLabel.Position = [51 133 52 22];
app.VO2maxEditFieldLabel.Text = 'VO2max';
% Create VO2max
app.VO2max = uieditfield(app.UIFigure, 'numeric');
app.VO2max.Position = [118 133 100 22];
% Create improvementLabel
app.improvementLabel = uilabel(app.UIFigure);
app.improvementLabel.HorizontalAlignment = 'right';
app.improvementLabel.Position = [28 93 75 22];
app.improvementLabel.Text = 'improvement';
% Create inprovement
app.inprovement = uieditfield(app.UIFigure, 'numeric');
app.inprovement.Position = [118 93 100 22];
% Create genderLabel
app.genderLabel = uilabel(app.UIFigure);
app.genderLabel.HorizontalAlignment = 'right';
app.genderLabel.Position = [60 51 43 22];
app.genderLabel.Text = 'gender';
% Create gender
app.gender = uieditfield(app.UIFigure, 'numeric');
app.gender.Position = [118 51 100 22];
% Create botton
app.botton = uibutton(app.UIFigure, 'push');
app.botton.ButtonPushedFcn = createCallbackFcn(app, @bottonButtonPushed, true);
app.botton.FontWeight = 'bold';
app.botton.Position = [308 176 100 22];
app.botton.Text = 'predication';
% Create intensityLabel
app.intensityLabel = uilabel(app.UIFigure);
app.intensityLabel.HorizontalAlignment = 'right';
app.intensityLabel.Position = [417 175 50 22];
app.intensityLabel.Text = 'intensity';
% Create intensity
app.intensity = uieditfield(app.UIFigure, 'numeric');
app.intensity.Editable = 'off';
app.intensity.Position = [482 175 100 22];
% Create timeLabel
app.timeLabel = uilabel(app.UIFigure);
app.timeLabel.HorizontalAlignment = 'right';
app.timeLabel.Position = [439 133 28 22];
app.timeLabel.Text = 'time';
% Create time
app.time = uieditfield(app.UIFigure, 'numeric');
app.time.Editable = 'off';
app.time.Position = [482 133 100 22];
% Create periodLabel
app.periodLabel = uilabel(app.UIFigure);
app.periodLabel.HorizontalAlignment = 'right';
app.periodLabel.Position = [428 93 39 22];
app.periodLabel.Text = 'period';
% Create period
app.period = uieditfield(app.UIFigure, 'numeric');
app.period.Editable = 'off';
app.period.Position = [482 93 100 22];
% 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 = ceshi
% 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
Thanks in advance!

Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!