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

1 view (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 7 Sep 2015
Edited: Walter Roberson on 7 Sep 2015
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 Comments
Muhammad Umer
Muhammad Umer on 8 Sep 2015
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 on 9 Sep 2015
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

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 7 Sep 2015
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 Comments
Muhammad Umer
Muhammad Umer on 7 Sep 2015
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 on 7 Sep 2015
Edited: Geoff Hayes on 7 Sep 2015
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.

Sign in to comment.

Categories

Find more on Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!