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

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

You do not appear to be writing to a text file: your fprintf() does not have a file identifier as the first parameter, and you are not passing a file identifier to NeuralNetwork() to write to.
Are you using diary() to write to a text file?
I edited the code, I am using the fprintf(fileID, '%d, %d, %d, %d, %d\n', lrate, trainingrate, n1, n2, MAPE) code for writing the text file and first row I am creating the text file. Without writing the text file on the command view I see different values for MAPE, but in the text file there must be a wrong thing.
Perihan - do you close the file with fclose?
MAPE is not calculated by any of the code you posted. Is NeuralNetwork doing an assignin('caller', 'MAPE', ...) or is it simply displaying the value ?
I closed the file but the result doesn't change. I added the code below.
fclose(fileID)
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

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)?
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일

0 개 추천

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