Matlab, automatically delete the index variable after the execution of a loop, Part 2

13 views (last 30 days)
suppose I have a loop
for i=1:10
then I want to clear the index variable 'i'. One way is to do
clear i
But is there a simpler way?
Robert Laws
Robert Laws on 2 Aug 2021
Edited: Robert Laws on 2 Aug 2021
It would be very useful if there was an option to have Matlab always clear the loop variable on exit from the loop. That is to say, the loop variable should have a scope only within the loop.
As things stand, the careful programmer has to put "clear loopvariable" after every closing "end" statement, which is tedious.

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 19 Dec 2020
That is the simpler way.

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!