How to use a neural network code and model developed using neural network fitting on a data set that is independent of the data set used to develop the neural network?

19 views (last 30 days)
I am having issues placing data independant of the data set into a neural network that I have developed using neural network fitting. It outputs the following code below. I would like to develop a neural network that others can use to estimate soil water content based upon some parameter inputs. Can anyone help me figure out how to develop a code in neural net fitting in which others can use to estimate something based on using the same imput parameter?
% Solve an Input-Output Fitting problem with a Neural Network
% Script generated by Neural Fitting app
% Created 06-Jan-2026 10:22:31
%
% This script assumes these variables are defined:
%
% KaBECSSAxbd_1 - input data
% WC_1 - target data
%
%% Prepare Inputs and Targets
x = KaBECSSAxbd_1';
t = WC_1';
%% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' - usually fastest
% 'trainbr' - slower, may be better for difficult problems
% 'trainscg' - lower memory usage
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation
%% Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize, trainFcn);
%% Choose Input and Output Pre/Post-Processing Functions
% For a list of all processing functions type: help nnprocess
net.input.processFcns = {'removeconstantrows', 'mapminmax'};
net.output.processFcns = {'removeconstantrows', 'mapminmax'};
%% Setup Division of Data for Training, Validation, and Testing
% For a list of all data division functions type: help nndivision
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'sample'; % Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
%% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse'; % Mean Squared Error
%% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = { ...
'plotperform', ...
'plottrainstate', ...
'ploterrhist', ...
'plotregression', ...
'plotfit'};
%% Train the Network
[net, tr] = train(net, x, t);
%% Test the Network
y = net(x);
e = gsubtract(t, y);
performance = perform(net, t, y);
%% Recalculate Training, Validation, and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net, trainTargets, y);
valPerformance = perform(net, valTargets, y);
testPerformance = perform(net, testTargets, y);
%% View the Network
view(net);
%% Plots
% Uncomment to enable plots
% figure, plotperform(tr)
% figure, plottrainstate(tr)
% figure, ploterrhist(e)
% figure, plotregression(t, y)
% figure, plotfit(net, x, t)
%% Deployment
% Change (false) to (true) to enable a deployment block
if false
% Generate MATLAB function for use in scripts or MATLAB Compiler
genFunction(net, 'myNeuralNetworkFunction');
y = myNeuralNetworkFunction(x);
end
if false
% Generate matrix-only MATLAB function for MATLAB Coder
genFunction(net, 'myNeuralNetworkFunction', 'MatrixOnly', 'yes');
y = myNeuralNetworkFunction(x);
end
if false
% Generate a Simulink diagram for simulation or deployment
gensim(net);
end

Answers (1)

Broy
Broy on 15 Jan 2026 at 6:03
Hi @Hans,
The script you posted is a Training Script. Its job is to build the brain. Look at the very bottom of your code under %% Deployment. Change false to true. Rename the function to something descriptive: EstimateSoilWater.
Run your script. You will see a new file appear in your Current Folder named EstimateSoilWater.m.
Now other people can use the file EstimateSoilWater.m as shown below:
user_inputs = [0.5; 12.3; 9.8; ... ];
estimated_WC = EstimateSoilWater(user_inputs);
Helpful Documentations:

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!