Confusing Matlab Artificial Neural Toolbox script
I don't see anything wrong in the generated code.
Let me explain:

In the following block, we define that 70% of the data will be used for training the network, 15% for the validation, and 15% for the testing:

net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;


Then the network is trained from the inputs/targets passed as arguments. Note that here, all inputs are given at once for efficiency. The separation training/validation/testing is done inside the net function from the proportions passed in the net input argument (through the net.divideParam.XRatio properties).

% Train the Network
[net,tr] = train(net,inputs,targets);


Then we test the results of the network on the data without discrimination between training/validation/testing. This performance score should be higher than the "real" performance score of the net as the performance over the training data should be higher than the one over the validation/testing data.

% Test the Network
outputs = net(inputs);  % Compute predictions from the inputs
errors = gsubtract(targets,outputs);  % Compare the predictions to the targets (true values)
performance = perform(net,targets,outputs)  % Built-in Matlab function giving an overall performance score


Now that we know how the net performs overall, we can look at how it performs on the training, validation, and testing sets, respectively. The masks are created as an output of the net function, from the proportions given in the first block of code. The output have been already all computed when the net function has been called.

% Recalculate Training, Validation and Test Performance
trainTargets = targets .* tr.trainMask{1}; % Apply a mask (0's and 1's to select the proper targets)
valTargets = targets .* tr.valMask{1}; % idem
testTargets = targets .* tr.testMask{1}; % idem

trainPerformance = perform(net,trainTargets,outputs); % Compute a score for the training data
valPerformance = perform(net,valTargets,outputs); % same for the validation data
testPerformance = perform(net,testTargets,outputs); % same for the test data


I think your code does the exact same thing but in more steps, with multiple calls to the net function and without using its native way of working (you define the proportions of training/validation/testing by hand for instance without making use of the net.divideParam.Xratio property). So it is probably not optimal from the computational point of view.