How to create an independent loop?

2 views (last 30 days)
Andrea Miceli
Andrea Miceli on 21 Sep 2021
Edited: Rik on 21 Sep 2021
hi everyone,
I try to explain my problem in the simplest way,
I wrote a code and I need a part of this code to be repeated 200 times. I tried a 'for loop', but that's not what I'm looking for because every time it runs a new loop the variables get intertwined.
I need this part of the code to be repeated 200 times as if I were running the code for the first time. (I need 200 repetitions, each independent of the other). Is it possible to do this on matlab? thanks for your help
Voss on 21 Sep 2021
Sounds like you need to restore your variables to their initial state on each loop iteration.

Sign in to comment.

Accepted Answer

Rik on 21 Sep 2021
Edited: Rik on 21 Sep 2021
If you want something to be independent, you need to use a function:
for n=1:200
function my_fun(n,vars)
%your code depending on n goes here

More Answers (0)


Find more on Loops and Conditional Statements 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!