Clear Filters
Clear Filters

How to integrate trained neural network in the app (I have referred to older posts but still not succeed).

5 views (last 30 days)
Dear Matlab Users,
I have refered to several answers by @kojiro Saito on how to integrate trained network in the app designer. One of the link that I refer is
https://www.mathworks.com/matlabcentral/answers/508824-how-do-i-integrate-a-trained-neural-network-into-an-application?s_tid=srchtitle
and I have followed his advice, but somehow the codes still not working. I hope someone could point out the mistakes that I made or how should I improve the codes.
first I add properties
properties (Access = private)
Property % Description
net;
ypredict;
end
then I add new function to load the network
function startupfunc(app)
net=load('net.mat');
app.net=net.net;
end
however here, received an error of Line 33: A METHODS block or END might be missing before the function definition. This might be causing additional error messages.
Next I tried to predict using the trained network by 4 variables input by the user.When calculate button is pressed, the new output shall be shown on the interface but no values appeared.
% Button pushed function: CALCULATEButton
function CALCULATEButtonPushed(app, event)
T=[app.Variable1.Value,app.Variable2.Value,app.Variable3.Value,app.Variable4.Value];
[T1 ps]=mapstd(T);% my network was trained in mapstd
ypredict=predict(app.net,T1);
yreverse=mapstd('reverse',ypredict,ps);
app.Output.Value=yreverse;
end
your kind assistance are much appreciated.

Answers (1)

UDAYA PEDDIRAJU
UDAYA PEDDIRAJU on 24 Nov 2023
Hi Nurliana,
I understand that you’re trying to integrate the trained neural network in the app designer. It seems that the error you encountered might be due to the function definition within the wrong section or a missing method block. To integrate a trained neural network in the app, you can follow these steps:
1). Load the Trained Network: Ensure that the function to load the network is within a methods block. You need to encapsulate the function within the "methods" section of your app.
methods (Access = private)
function loadTrainedNetwork(app)
net = load('net.mat');
app.net = net.net;
end
end
2). Predict Using the Trained Network: When the "Calculate" button is pressed, ensure that the input is pre-processed as per the training data, and then use the trained network to predict the output.
methods (Access = private)
function calculateButtonPushed(app, event)
T = [app.Variable1.Value, app.Variable2.Value, app.Variable3.Value, app.Variable4.Value];
[T1, ps] = mapstd(T); % Assuming the network was trained using mapstd
yPredict = app.net(T1);
yReverse = mapstd('reverse', yPredict, ps);
app.Output.Value = yReverse;
end
end
By encapsulating the load function within the methods block and ensuring proper preprocessing and prediction steps in the “calculateButtonPushed” function, you should be able to integrate the trained neural network into your app successfully.
You can also refer to the https://www.mathworks.com/matlabcentral/answers/444474-neural-network-in-matlab-app-designer.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!