i have to run .m file 51 times. how can i save all 51 results in one file?

1 次查看(过去 30 天)
The program contains 3 functions:
function algorithm code,
function bounds,
function problem code,
In result it creates a .mat file with 1 value. i have tried several options to do multiple runs with multiple outputs in 1 file like on every run it adds new value in file but it couldn't works .
Is there any way to solve this thing?

采纳的回答

Walter Roberson
Walter Roberson 2015-9-7
编辑:Walter Roberson 2015-9-7
function f = multilop() %store this in multiop.m
for k=1:51,
algresult(k,:) = algorithmcode();
boundsresult(k,:) = bounds();
problemresult(k,:) = problem();
end
end
%this part can be in algorithmcode.m or in multiop.m
function aresult = algorithmcode()
aresult = .... whatever appropriate
end
%this part can be in bounds.m or in multiop.m
function bresult = bounds()
bresult = ... whatever appropriate
end
%this part can be in problem.m or in multiop.m
function presult = problem()
presult = ... whatever appropriate
end
  4 个评论
Muhammad Umer
Muhammad Umer 2015-9-8
This part
%this part can be in algorithmcode.m or in multiop.m
function aresult = algorithmcode()
aresult = .... whatever appropriate
end
%this part can be in bounds.m or in multiop.m
function bresult = bounds()
bresult = ... whatever appropriate
end
%this part can be in problem.m or in multiop.m
function presult = problem()
presult = ... whatever appropriate
end
Walter Roberson
Walter Roberson 2015-9-9
Take your code for "function algorithm code" and write it in a file named algorithmcode.m . Take your code for "function bounds" and write it in a file named "bounds.m". Take your code for "function problem code" and write it in a file named "problemcode.m".
In each case, the name of the file must exactly match the name of the function on the "function" line, which must be the first line of the file that is not blank or a comment.
In each case, the function name must start with a Latin Alphabet letter, A through Z or a through z. The remaining characters of the function name must be one of those Latin Alphabet letters, or one of the digits 0 through 9, or the underscore character '_' . It is not permitted to have blanks in the name of the function.
function 2do %not allowed! name must start with letter
function problem code %not allowed! name must not have spaces
function problemcode %allowed

请先登录,再进行评论。

更多回答(1 个)

Geoff Hayes
Geoff Hayes 2015-9-7
Muhammad - try saving the results of each iteration to an array and then, once the last iteration has completed, write this array to the mat file.
  4 个评论
Muhammad Umer
Muhammad Umer 2015-9-7
how i need to call these functions with for loop. i have tried this way but it is not working.
function f=mutltilop()
for k=1:10,
algorithmcode()
bounds()
problem()
end
end
algorithncode()
bounds()
problem()
error:
Function with duplicate name "algorithm" cannot be called.
??? Maximum recursion limit of 500 reached.Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can
crash MATLAB and/or your computer.
Error in ==> algorithm
2nd
for k=1:10,
algorithm code()
bounds()
problem()
end
error:
Function definitions are not permitted in this context.
Geoff Hayes
Geoff Hayes 2015-9-7
编辑:Geoff Hayes 2015-9-7
Muhammad - typically, the error
Maximum recursion limit of 500 reached.Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.
indicates that a function is calling itself recursively (without a "stopping" condition). For example, the following code would generate the same error
function algorithm
algorithm
If the above is saved to a file called algorithm.m and then called from the command line, then we would observe the same error that you have described. Have you perhaps done something similar in your algorithm function?
As for the second error
Function definitions are not permitted in this context.
this generally happens if you write a function signature after you have already written some code within your script. For example, if the following is saved to a file called myScript.m
% some dummy code
t = 42;
function myFunction(a,b,c)
and then I try to execute the script, I would see the same error as you. Have you written something similar in your code?
Please post some or all of your code so that we can get an idea of what you have written and what can be done so that you can obtain the results that you desire.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by