How can I write different ouput values for each for-loop?

2 次查看(过去 30 天)
I am trying to write the outputs in a text file using with the for loop and a function. After working the code in thee workspace I see the outputs for MAPE values at the each loop but in the text file it gets the one value it does not change. I use neural network function for MAPE values.How can I write the different MAPE values for each loop in the text file?
fileID=fopen('Donguu.txt','w+');
for lrate=0.1:0.2:0.2
for trainingrate=0.2:0.4:0.8
for n1=3:2:7
for n2=6:2:10
NeuralNetwork(Input, Target, trainingrate, n1, n2, lrate);
fprintf(fileID, '%d, %d, %d, %d, %d\n', lrate, trainingrate, n1, n2, MAPE);
end
end
end
end
lrate=6.000000e-01, trainingrate=2.000000e-01, n1=3, n2=6
MAPE =
0.6875
lrate=6.000000e-01, trainingrate=2.000000e-01, n1=3, n2=8
MAPE =
0.6354
lrate=6.000000e-01, trainingrate=2.000000e-01, n1=3, n2=10
MAPE =
0.7353
  6 个评论
Perihan Bilgeç
Perihan Bilgeç 2018-9-10
I closed the file but the result doesn't change. I added the code below.
fclose(fileID)
Perihan Bilgeç
Perihan Bilgeç 2018-9-10
编辑:Walter Roberson 2018-9-10
Yes I calculated the MAPE value in the function that named NeuralNetwork. The function is below with input and output arguments.
function [net ye Yv MAPE R2] = NeuralNetwork(Input, Target, trainingrate, n1, n2, lrate)

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-9-10
You need to change
NeuralNetwork(Input, Target, trainingrate, n1, n2, lrate);
to
[~, `, ~, MAPE, ~] = NeuralNetwork(Input, Target, trainingrate, n1, n2, lrate);
  2 个评论
Perihan Bilgeç
Perihan Bilgeç 2018-9-11
I tried and it is correct now. Thank you very much for the help. What is the wrong thing about the usage of NeuralNetwork(Input, Target, trainingrate, n1, n2, lrate)?
Walter Roberson
Walter Roberson 2018-9-11
NeuralNetwork(Input, Target, trainingrate, n1, n2, lrate)
calculates an internal variable named MAPE but does not assign to MAPE in the workspace of the calling function.
Any assignment you make inside a function definition does not affect the calling function, except for one of the following circumstance:
  • you declared the variable as global in all applicable places; or
  • you are using nested functions with shared variables; or
  • you use assignin('caller') -- and if you do then increasingly MATLAB might refuse to recognize the assignment unless you already had assigned to a variable of the same name
  • you are using a handle object and setting a property of the handle object. handle objects act similar to pointers
  • you are calling the function from the command line or from a script that is being invoked from the command line or being invoked as-if it was from the command line (callbacks specified as strings are invoked as-if from the command line), and you use assignin('base') -- and if you do then increasingly MATLAB might refuse to recognize the assignment unless you already had assigned to a variable of the same name
All this applies even for variables listed as output variables. For example,
function primus
A = 5;
secondus();
disp(A)
function A = secondus()
A = 22;
Then it will be 5 that is displayed, not 22, because the A inside secondus is not the same variable as the A inside primus. If primus() contained the line
A = secondus();
then A would change to 22, but not because the variable names happened to be the same: assignment is strictly positional, so the first output of secondus is assigned to the first location listed on the left side of the assignment.

请先登录,再进行评论。

更多回答(1 个)

Perihan Bilgeç
Perihan Bilgeç 2018-9-11
Thank you very much for detailed explanation.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by