File Exchange Pick of the Week

Our best user submissions

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.


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

Published with MATLAB® 7.10



To leave a comment, please click here to sign in to your MathWorks Account or create a new one.