i have to run .m file 51 times. how can i save all 51 results in one file?
2 次查看(过去 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?
0 个评论
采纳的回答
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 个评论
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
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 个评论
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 Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!