MATLAB: How to create a function to dump all the local variables in a function to the base workspace in MATLAB 7.8 (R2009a)

assiginbasedebugdumpevalinfunctionlocalMATLABvariableworkspace

I am doing some debugging and I would like a utility that will automatically dump all the local variables into the base workspace for analysis. I want a utility to do this, not nested functions.

Best Answer

This can be done using the EVALIN and ASSIGNIN commands.
function to_base_workspace()
ws=evalin('caller','whos()');
for i=1:length(ws)
tmp=evalin('caller',ws(i).name);
assignin('base',ws(i).name,tmp);
end
If you save the code above into a file named "to_base_workspace.m", you can then run the following example code:
function example
a=rand(10,10);
b='here is an example string';
c=num2cell(1:10);
to_base_workspace();