save all variables in function scope to main workspace

  • Follow


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)
0
Reply Ross 4/8/2010 10:44:05 PM

Ross Anderson 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?

who() will return a cell array of strings of the variable names.
0
Reply Walter 4/8/2010 10:49:56 PM


"Ross Anderson" <rpa5nospam@cornell.edu> wrote in message <hplm7l$71l$1@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)

why not just SAVE it to a MAT-file; possibly within a CATCH statement(?)...

us
0
Reply us 4/8/2010 10:51:06 PM

Dear Ross!

> 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)

What about a simple breakpoint in the debugger or a KEYBOARD command before RETURN?!

But if you really want to export the variables:
  A = who;
  for i = 1:length(A)
    assignin('base', A{i}, eval(A{i}));
  end
I confirm, that this isn't good practice. But I really appreciate well debugged programs also.

Jan
0
Reply Jan 4/8/2010 10:55:37 PM

"Ross Anderson" <rpa5nospam@cornell.edu> wrote in message <hplm7l$71l$1@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)

If you want to save all variables, a simple way is to just
do a save. Then read in the mat file back in the base
workspace.

I recently posted a tool called putvar onto the File
Exchange, which can save a few selected variables back
into the base workspace. I've just been modifying the
code to allow more capabilities.

John
0
Reply John 4/9/2010 2:13:05 AM

In article <hplm7l$71l$1@fred.mathworks.com>,
 "Ross Anderson" <rpa5nospam@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.
0
Reply Doug 4/9/2010 3:41:38 AM

"Ross Anderson" <rpa5nospam@cornell.edu> wrote in message 
news:hplm7l$71l$1@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. 


0
Reply Bob 4/9/2010 6:13:41 PM

6 Replies
194 Views

(page loaded in 0.024 seconds)

Similiar Articles:













7/20/2012 7:28:15 AM


Reply: