I want to clear all variables in my code except the loop index after every iteration. for example
for i=1:20
c= i+20;
save c;
clearvars -except i
end
How do I achieve this in Python?
A good solution is to define a function, and then only return the variables you want to keep. In Python, variables that are defined within the scope of a function are removed when the function is done executing.
def my_for(i):
for j in range(1, i+1):
c = j + 20
return i