9

On Matlab, when i am using a "for...end" loop, the indexing variable still exists in my workspace after the loop have been fully executed. I would like it to be automatically deleted, since its not relevant anymore outside of the loop and that it pollutes the workspace.

For example, in the following code, the variable "i", still exists after the execution of the loop. Since it should be a local variable, I would like it to be deleted automatically without me having to do it explicitely.

List = [1 2 3 4] ;

for i = List
   fprintf('value = %i\n', i) ; 
end
% "i" still exists, while its outside of its context

clear i; % I would like to avoid doing this everytime I exit a for..end

I know it is more of an aesthetic issue than a bug, but for an easier understanding of the results of my program, I would like those "temporary" variables to disappear when I exit their contexts.

So far, I only was able to reduce the number of those temporary variables by reusing them.

Edit:

It seems that there is no real solution to automatically remove those "temporary" variables. The closest ways to avoid having those variables are:

  • Avoiding loops

  • Make the loops in functions, the variables of the functions are local and won't get in the workspace.

Mesop
  • 5,187
  • 4
  • 25
  • 42
  • 3
    Unrelated: I recommend using `ii` as the index variable to avoid clobbering `i` (the imaginary unit). – nibot Mar 24 '12 at 11:12
  • @nibot: +1, even though one can also make the habit of writing the imaginary unit as `1i` (note the missing space), if you use it a lot less than loop variables ... – Jonas Heidelberg Mar 24 '12 at 21:31
  • Yeah, that's true; I only learned of 1i recently. – nibot Mar 24 '12 at 22:13

2 Answers2

6

If you REALLY want to make sure that some of your variables have limited scope, and you want to avoid calling clear, you can use nested functions. Note that this may not help with readability, and it is more typing than calling clear. However, it does make sure that the only variables in your main function workspace are the ones that you want/need to remain.

function doSomething

List = [1 2 3 4] ;

runLoopOnList()

%# some other code here



   %# nested functions
   function runLoopOnList
      %# i, and any other variable defined here
      %# will not appear in the workspace
      %# in contrast, all variables in the workspace
      %# are visible and can be changed by the nested function
      %# If a nested function should assign a new important
      %# variable in the main workspace, have it return
      %# and output. 
      for i = List
         fprintf('value = %i\n', i) ; 
      end
   end %# nested function
end %# main function
Jonas
  • 74,690
  • 10
  • 137
  • 177
5

Look ! No loop, no iteration variable !

fprintf('value = %i\n', List)

And, while I'm here, I disagree that your i is a temporary variable; you've put it in the workspace so it is, essentially, global. Put it in another context (e.g. inside a function) if you don't want it to 'pollute' the workspace.

And yes, I know that Matlab has a concept of global variables which is slightly different from workspace variables, but it's not quite relevant here.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • Thank you, i never thought of writing the fprintf that way. Is there a way (without using functions) to use a variable without putting it in the workspace? – Mesop Mar 24 '12 at 12:05
  • @olchauvin: Not really. It can occasionally be useful actually when tracking down bugs and the like. – PearsonArtPhoto Mar 24 '12 at 13:04
  • Ok, this works for this specific example. In general the answer would be: No, this is not possible? – Bernhard Mar 25 '12 at 10:42
  • 1
    @Bernhard: Looking at the answers we have so far, it seems that, except by creating a function and running the loop in it, its not possible. I haven't found something like we have in C (putting {} around a section of the the code), to create a context for the variables. – Mesop Mar 25 '12 at 11:51