1

I have a couple questions regarding MATLAB workspaces:

  1. When does MATLAB decide to change the workspace and what conditions prompt it?

  2. Something strange is happening in the following example. I run it with a breakpoint at line 4, then step using F10 to watch the workspace variables. Clearly, I see that m is deleted within the first iteration of the inner loop, but somehow MATLAB still knows to go to the next iteration in the outer loop!

Example:

something = 2;
somethingelse = 3;
for m = 1 : something
    for n = 1 : somethingelse
        %do something
        clearvars -except n something somethingelse % This clears m, but it still functions
    end
end

The only thing I can think of is that MATLAB probably has not updated the memory locations given that the variable does not show on the list.

gnovice
  • 125,304
  • 15
  • 256
  • 359
Numbers682
  • 129
  • 1
  • 11
  • 1
    Each function has its own workspace. Clearing `for` variables: WTF. Also, the for is defined, so even if you clear the variable, the for already knows how to iterate. – Ander Biguri Jun 12 '17 at 16:07
  • So to take home: when you declare `m` in the for loop, it is not exactly the same as decalring `m` outside the for loop. But please, why would you want to delete the loop variable? That can only lead to disastrous code. – Ander Biguri Jun 12 '17 at 16:17
  • Looks like he's clearing it for the sake of example, not because he's a blithering idiot. – toshiomagic Jun 12 '17 at 16:23
  • 1
    @toshiomagic But then it is a very bad example, as this "weird" behaviour only appears because of deleting the loop variable. Any other case would not result in this behaviour – Ander Biguri Jun 12 '17 at 16:27
  • Elitist developers on this website who bash newer people for their curiosity is toxic. – toshiomagic Jul 25 '17 at 13:24

2 Answers2

3

In MATLAB there are generally two types of workspaces: the base workspace and function workspaces. The base workspace is what you use when you enter commands at the command line, or when you run scripts (all of which share the base workspace). Conversely, each function has its own unique workspace. Unlike C or C++ (or a number of other languages) you don't have any scoping of variable within loops or conditional structures, just one unique workspace for each instance of a function.

The issue you're seeing in your example isn't really related to this, it's just an artifact of how for loops behave in MATLAB. Taken from the "Tips" section:

Avoid assigning a value to the index variable within the loop statements. The for statement overrides any changes made to index within the loop.

In other words, once an iteration of the loop completes and returns to the beginning, MATLAB ignores any changes to the loop variable and simply increments it to the next value.

If you'd like to learn more details about MATLAB workspaces and scoping, I'd check out these links:

gnovice
  • 125,304
  • 15
  • 256
  • 359
1
  1. Matlab changes workspace to the current scope.

  2. You've only cleared the value of m within the scope of the second loop.

Try adding p = m+n after the clearvars command within the second loop. Since you've cleared m only within the scope of the n loop, you cannot use it. However, you did not remove m from the scope of the m loop.

Also, since the m for loop exists within the scope of your base workspace, you can clear m within the m for loop all you want, the loop will always have access to it. That's why, if you remove the clearvars line, when you return to the base workspace, you can see m and n equal to something and somethingelse respectively.

What I think you're looking for is a better explanation of Matlab's memory management, which you can find here: MATLAB's Garbage Collector?

toshiomagic
  • 1,335
  • 1
  • 12
  • 39