How can I write different ouput values for each for-loop?
1 次查看(过去 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 个评论
采纳的回答
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 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!