Matlab, automatically delete the index variable after the execution of a loop, Part 2
Show older comments
suppose I have a loop
for i=1:10
T(i,:)='NY';
end
then I want to clear the index variable 'i'. One way is to do
clear i
But is there a simpler way?
6 Comments
John D'Errico
on 19 Dec 2020
Edited: John D'Errico
on 19 Dec 2020
How is this part 2, since you asked identically the same question on stackoverflow? Is this just a general survey of every possible site, in case you can get a different answer? You cannot automatically delete a loop index variable after a loop. This is part of the language, and expected.
The simple answer is to just learn to use functions, then all of your spurious variables go away.
Image Analyst
on 19 Dec 2020
I'd not even bother. In these days of computers with 32 billion bytes of RAM, why bother clearing 8 bytes?
Anyway how much simpler to you want? It's only 6 characters on one line of code. It won't get any simpler.
John D'Errico
on 19 Dec 2020
I agree. Clearing the variable is just a waste of CPU cycles, however you might do it. Why bother?
Steven Lord
on 20 Dec 2020
then I want to clear the index variable 'i'
Why? What benefit do you hope to gain from doing this?
alpedhuez
on 20 Dec 2020
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.
Accepted Answer
More Answers (0)
Categories
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!