From: Doug Schwarz on
In article <hplm7l$71l$1(a)fred.mathworks.com>,
"Ross Anderson" <rpa5nospam(a)cornell.edu> wrote:

> I'd like to save all of the variables in my function to the main workspace
> for debugging purposes. I know I can do something like
>
> function ret = myfunc
> assignin('caller','somevariable',4);
> ret=5;
>
> But say I want to do this with ALL variables in the function scope, not just
> 'somevariable'
> How would I do that?
>
> (I know this isn't good practice, but I'm running my function in a loop. It
> goes awry sometimes, but I don't want to return EVERYTHING in order to find
> out why, so displaying matrices at the command line after an error would be
> nice)

I use this function sometimes. You can pass in variable names or leave
them out in which case all variables are exported.

%------------ export2base.m -------------------
function export2base(varargin)
%export2base: Export variables to base workspace.

% Douglas M. Schwarz

% Get names of variables in caller's workspace
w = evalin('caller','who');

% Remove 'ans' if it is present.
w = setdiff(w,'ans');

% Keep only variables listed in input arguments that actually exist in
% caller's workspace.
if ~isempty(varargin)
w = intersect(w,varargin);
end

% Loop through variables and put them in base workspace.
for i = 1:length(w)
assignin('base',w{i},evalin('caller',w{i}))
end

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
From: Bob Gilmore on
"Ross Anderson" <rpa5nospam(a)cornell.edu> wrote in message
news:hplm7l$71l$1(a)fred.mathworks.com...
> I'd like to save all of the variables in my function to the main workspace
> for debugging purposes. I know I can do something like
>
> function ret = myfunc
> assignin('caller','somevariable',4);
> ret=5;
>
> But say I want to do this with ALL variables in the function scope, not
> just 'somevariable' How would I do that?
>
> (I know this isn't good practice, but I'm running my function in a loop.
> It goes awry sometimes, but I don't want to return EVERYTHING in order to
> find out why, so displaying matrices at the command line after an error
> would be nice)

Ross,
Run...
>> doc dbstop
and pay attention to the "dbstop if error" and "dbstop if error identifier"
parts. That will help you drop into the debugger "when things go awry."
Once you're in the debugger, you'll have full access to the function
workspace.

Hope that helps,
--
Bob Gilmore, The MathWorks, Inc.