Writing result of script (ran in a loop) into vector

1 view (last 30 days)
Jonas
Jonas on 9 Oct 2020
Commented: Rik on 12 Oct 2020
Hey,
I want to execute a script (main) in a loop which gives me a different result (solvertime) every time I run it and write this result into a vector with the size of the Number of runs.
NumOfRuns = 3;
results=zeros(1,NumOfRuns);
for i=1:NumOfRuns
main;
results(i)=solvertime;
end
For some reason it gives me this vector. Help is much appreciated.
  4 Comments
Rik
Rik on 12 Oct 2020
Regarding your flag ("I found out that the answer to this question was that "i" was somewhere in the script. so no one in this forum could know the answer. delete this please"):
We tend not to delete questions (especially those that have received valid attempts at an answer) unless there are very good reasons for doing so. Often the underlying reason is that the content should never have been made public in the first place. Because it is hard to say if something is usefull to some random passer-by in the future, we tend to want to keep content as-is. I don't see any such reason for deletion in your post, so I'm going to remove the flag. If you disagree, feel free to explain in a comment why you believe this question should be deleted. Just in case you were planning to delete the question content: I can easily restore it from this Wayback Machine capture, so you don't have to bother.
(Even though I do have some editorial priviliges through the reputation system, I am not afiliated with Mathworks and don't control anything on this website. Feel free to contact support if you want Mathworks to make a decision about this post.)

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 9 Oct 2020
Something does not make much sense: given NumOfRuns = 3, why does results have 26 columns?
I suspect that the script also contains a variable named results, in which case there are two main solutions:
  1. rename one/both of those variables so that they have different names
  2. turn your script into a function (preferred approach).
  2 Comments
Jonas
Jonas on 9 Oct 2020
hey,
  1. renaming the variables in this script (as shown above, not main) have the same results
I guess it has something to do with script (main) because it is quite complex and it interferres with that loop function in some way
Stephen23
Stephen23 on 9 Oct 2020
Edited: Stephen23 on 9 Oct 2020
This is why experienced MATLAB users avoid scripts for any real work. Scripts are fun and easy to play with, but if you want reliable, repeatable, reuable code then functions/classes are the only way to go.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!