Code covered by the BSD License  

Highlights from
save_to_base.m

5.0

5.0 | 3 ratings Rate this file 42 Downloads (last 30 days) File Size: 1.51 KB File ID: #24121

save_to_base.m

by Phillip M. Feldman

 

14 May 2009 (Updated 18 Aug 2009)

copies variables from the workspace of the calling function to the base workspace

| Watch this File

File Information
Description

save_to_base() copies variables in the workspace of the calling function to the base workspace. This makes it possible to examine a function's internal variables from the Matlab command prompt after the calling function terminates.

If the optional argument "overwrite" is present and has a non-zero value, variables in the workspace of the caller will overwrite variables in the base workspace having the same name. Otherwise, preexisting variables in the base workspace will not be overwritten.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
16 Aug 2009 Donald

I found trouble when I had a stray "ans" variable floating around. I got this message:

??? The variable "ans" is undefined.

To avoid trying to copy "ans", I modified save_to_base.m so that the for loop now reads:

for i= 1 : length(variables)
    if ~strcmp(variables{i},'ans')
        tmp= evalin('caller',variables{i});
        assignin('base',variables{i},tmp);
    end
end

Very nice little function.

20 May 2011 Jeff Miller  
02 Aug 2011 Benjamin  
12 Jan 2012 Arsalan

For me it does not work. I have a function that has some variables to save in workspace. I use evali('base',VarName) but that does not work. Just after file execution, all variables disappear from workspace

Please login to add a comment or rating.
Updates
02 Jun 2009

This file replaces save_variables, and fixes two problems:

- It is no longer necessary to nest the function.
- A calling argument has been added to control whether preexisting variables in the base workspace are overwritten.

18 Aug 2009

Added patch to exclude 'ans' variable.

Tag Activity for this File
Tag Applied By Date/Time
save variables Phillip M. Feldman 15 May 2009 10:12:30
base workspace Phillip M. Feldman 15 May 2009 10:12:30
debugging Phillip M. Feldman 15 May 2009 10:12:30

Contact us at files@mathworks.com