From: Ross Anderson on
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)
From: Walter Roberson on
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.
From: us on
"Ross Anderson" <rpa5nospam(a)cornell.edu> wrote in message <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)

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

us
From: Jan Simon on
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
From: John D'Errico on
"Ross Anderson" <rpa5nospam(a)cornell.edu> wrote in message <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)

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