Step 6:  

Test the trained network.

clear;
clc;
close all;

net=feedforwardnet([20 20 20]);
net.layers{1:3}.transferFcn = 'poslin'; 
net.layers{4}.transferFcn = 'softmax';  %output layer
 
X= zeros(5, 5, 5); %create a placeholder for input data (initialize X).

X(:,:,1) = [0 1 1 0 0   %1
                0 0 1 0 0
                0 0 1 0 0
                0 0 1 0 0
                0 1 1 1 0];

X(:,:,2) = [1 1 1 1 0   %2
                0 0 0 0 1
                0 1 1 1 0
                1 0 0 0 0
                1 1 1 1 1];

X(:,:,3) = [1 1 1 1 0   %3
                0 0 0 0 1
                0 1 1 1 0
                0 0 0 0 1
                1 1 1 1 0];
 
X(:,:,4) = [0 0 0 1 0   %4
                0 0 1 1 0
                0 1 0 1 0
                1 1 1 1 1
                0 0 0 1 0];

X(:,:,5) = [1 1 1 1 1   %5
                1 0 0 0 0
                1 1 1 1 0
                0 0 0 0 1
                1 1 1 1 0];
        
D = [1 0 0 0 0     %correct answer matrix used to train network
        0 1 0 0 0
        0 0 1 0 0
        0 0 0 1 0
        0 0 0 0 1];

for k=1:5  
newX(:,k)= reshape(X(:,:,k), 25, 1);
end;


net.trainparam.goal =0;      % This is our idea goal, which means that we'd like the error to be zero (this may not happen, but it's a goal).
net.divideparam.valratio = 0;   %This means don't set aside part of the trianing data for "validation". 
net.divideparam.trainratio =1;  %This means use all the training data (the four rows) for training
net.divideparam.testratio =0;   %This means don't set aside any of the training data for an independent test
net.trainparam.min_grad = 1e-100; 

net=train(net,newX,D); 

% Lets test the trained network with one of the digits, for example the
% digit 3 which is conveniently available as the third column of newX.
% In the following line, we should expect y to be a 5-number numeric code:
% [0 0 1 0 0] which corresponds to the correct numeric code for the third
% digit (each of those values is associated with one output node)

y=net(newX(:,3))

%With so few training trials, the network may not always perform perfectly,
%especially if the starting weights are way off, but generally the network
%does produce the correct final vector [0 0 1 0 0]

% Here is where you may see an interesting thing, results that may look something
% like [0.5 0.5 1 0.5 0.5] instead of the expected
[0 0 1 0 0].  You will notice that
% the sum of the output nodes don't add up to 1 even though you are using the
% Softmax activation function.
% This has to do with a "quirky" feature of Matlab when using feedforwardnet to
% create a network. AFTER the final softmax layer, matlab does some additional
% post-processing computations (with the goal of scaling the output values so they 
% are more suitable for training, e.g., avoiding outliers, etc.).  This is done after
% the softmax activation function and it is called the "Output Processing Function".
% This is "hidden" from you and done automatically so you don't usually notice it.
% You can see the current value of this function by typing net.output.processFcns in
% the command window.  You can fix this problem (i.e., get rid of the Output
% Processing Function) by adding the following line to your program after you've
% created the network:
% net.output.processFcns = {};
% If you add this line, then you'll see that the final output of your network sums
% to 1 when you add all the final values at the output nodes after the application of
% the softmax activation function.