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.
要发表评论，请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。