File Exchange Pick of the Week

May 7th, 2010

Putting Variables Back in the Base Workspace

Jiro's pick this week is PUTVAR by John D'Errico.

This is an interesting one. The more I think about it, the more I find it useful. putvar is a simple function for saving local variables to the base workspace, without requiring any output arguments to your function. It works similarly to assignin, but it can do multiple variables at once.

In general, my stance is that I prefer not using commands like assignin, eval, or evalin to create variables because there are usually other more programmatically sound ways to achieve the same end goal (cell arrays, structures, output arguments to a function, etc). However, the particular use case John mentions in the HELP resonates with me. When I'm in the debugger, I sometimes want to save out some of the variables in the function back into the base workspace so that I have them available after I exit debug mode. Instead of making a bunch of assignin calls for all the variables of interest:

  assignin('base', 'var1', var1)
  assignin('base', 'var2', var2)
  assignin('base', 'var3', var3)
  assignin('base', 'var4', var4)

I can simply make a single call to putvar:

  putvar(var1, var2, var3, var4)

As usual (and expected), John's code is very well written -- nice help text with examples, extensive comments, error checking, and warnings.

Comments

Give this a try and tell us what you think, or leave a comment for John on the submission page.


Get the MATLAB code

Published with MATLAB® 7.10

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.