title: Matlab variable count limit
body: <p>I have some C++ code that communicates with Matlab via the Engine C API.  My code creates temporary variables in the Matlab workspace, which it diligently cleans up via <code>clear</code> calls as soon as possible.  However, at some point, my application fails, telling me that it is unable to create the next Matlab temporary variable (usually after ~65530 such operations).</p>
  
  <p>After some experimentation on the Matlab command line, I discovered I could recreate this problem in pure Matlab (that is, independent of my C++ code and its use of the Engine API).  Consider the following code:</p>
  
  <pre><code>for i = 1 : 100000
    eval(sprintf('x_%d = %d', i, i));
    whos
    eval(sprintf('clear x_%d', i));
    whos
  end
  </code></pre>
  
  <p>Executing this code on my 32-bit windows laptop with Matlab R2008B (ancient, I know), the loop eventually aborts with the error message: </p>
  
  <blockquote>
    <p>The current workspace already has too many variables; there is no room
    for "x_65532".</p>
  </blockquote>
  
  <p>So, it seems that at least this obsolete version of Matlab has a 64K symbol table limit.  Perhaps the limit is bigger on newer (64-bit) versions of Matlab--I'd be interested to hear what results others get.</p>
  
  <p>However, the more interesting question is what effect the <code>clear</code> call is having and how to work around its odd behavior.  Here is the output from an iteration a little prior to the abort:</p>
  
  <pre><code>x_65530 =
  
         65530
  
    Name         Size            Bytes  Class     Attributes
  
    i            1x1                 8  double              
    x_65530      1x1                 8  double              
  
    Name      Size            Bytes  Class     Attributes
  
    i         1x1                 8  double              
  </code></pre>
  
  <p>As you can see, the <code>whos</code> output clearly shows that the temporaries from the prior iterations have been removed from the workspace, and the clear seemingly works as expected.  Nonetheless, the symbol table has apparently reached capacity.</p>
  
  <p>So, two questions for the SO faithful:</p>
  
  <ol>
  <li>How can I work around this somewhat arbitrary limit?  That is, what should my <code>clear</code> calls be replaced with?</li>
  <li>Has this behavior changed with newer and/or 64-bit versions of Matlab?</li>
  </ol>