No BSD License  

Highlights from
ToSavedWorkspace

Be the first to rate this file! 2 Downloads (last 30 days) File Size: 11.9 KB File ID: #7642
image thumbnail

ToSavedWorkspace

by Michael Robbins

 

11 May 2005 (Updated 03 Jun 2005)

Saves the workspace variables to the structure

Editor's Notes:

This file was a File Exchange Pick of the Week

| Watch this File

File Information
Description

TOSAVEDWORKSPACE Saves the workspace to a structure

This trivial function saves all of the workspace variables to a structure.
 
The intent of this script is to record the workspace variables before performing an operation on them, so as to keep them available after the operation for comparison.
 
This code also has a built in test scaffold so it is an example of how to build self-testing code.
 
TOSAVEDWORKSPACE returns a structure who's fields are all of the variables in the workspace.
 
TOSAVEDWORKSPACE VARIABLES returns a structure who's fields are VARIABLES.
 
TOSAVEDWORKSPACE -CLEAR returns a structure who's fields are all the variables in the workspace and then clears the workspace, leaving only the structure.
 
TOSAVEDWORKSPACE -xVARIABLE returns a structure who's fields are all the variables in the workspace except VARIABLE
 
TOSAVEDWORKSPACE -xVARIABLE1 -xVARIABLE2 returns a structure who's fields are all the variables in the workspace except VARIABLE1 and VARIABLE2
 
S = TOSAVEDWORKSPACE(...) returns a structure who's fields are all of the variables in the workspace.
 
S = TOSAVEDWORKSPACE('-TEST') runs a test script.
 
Example: Compare the workspace before and after executing an operation
  clear all; % start with a clean workspace
  A = floor(10.*rand(1,100)); % generate some data
  TSW = ToSavedWorkspace; % save the workspace
  A = floor(A.*2); % perform an operation
  setdiff(A,TSW.A) % compare the workspace
 
Example: Save the workspace and clear all variables
  clear all; % start with a clean workspace
  a = rand(10); % create some variables
  b = a.*2;
  c = ones(1,10);
  who % what's in the workspace?
  TSW=ToSavedWorkspace('-CLEAR'); % save the workspace & clear
  who % only TSW is left
  TSW % the previous workspace is in TSW
 
Example: Save the variables a and b, but no others
  clear all; % start with a clean workspace
  a = rand(10); % create some variables
  b = a.*2;
  c = ones(1,10);
  TSW=ToSavedWorkspace('a','b'); % save a & b
 
Example: Save all variables but b
  clear all; % start with a clean workspace
  a = rand(10); % create some variables
  b = a.*2;
  c = ones(1,10);
  TSW = ToSavedWorkspace('-xb'); % save all but b
 
Example: Save the variables a and c, then clear the workspace
  clear all; % start with a clean workspace
  a = rand(10); % create some variables
  b = a.*2;
  c = ones(1,10);
  TSW = ToSavedWorkspace( ... % save a & c then clear
     'a','c','-CLEAR');
 
Example: Save the variables a and c, then remove c
  clear all; % start with a clean workspace
  a = rand(10); % create some variables
  b = a.*2;
  c = ones(1,10);
  TSW = ToSavedWorkspace( ... % save a & c then clear
     'a','c');
  TSW % TSW has fields a and c
  TSW = rmfield(TSW,'c'); % eliminate field c
  TSW % TSW now only has field a
 
Example: Compare two files
  clear all; % start with a clean workspace
  load('Test1'); % load a file
  t1=ToSavedWorkspace('-CLEAR'); % save & clear the workspace
  load('Test2'); % load another file
  t2 = ToSavedWorkspace('-xt1'); % save the workspace, excluding t1
  keep t1 t2 % clear the workspace
                    % Keep is by Xiaoning (David) Yang
                    % and is available on the MATLAB
                    % File Exchange
 
IT'S NOT FANCY, BUT IT WORKS
 
See also clear, who, keep, varargin, nargin, test, rmfield

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
development, development environment, keep, save, structure, utilities, variable
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
17 Aug 2005 Fabio Wegmann

I also want to participate in the reunion of the 'save workspace' family, see STAKK: http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7096

12 May 2005 urs (us) schwarz

michael, nice stuff - although it reminds just a bit of my pedestrian GCOLL, which, in essence, does the same thing
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=3967&objectType=FILE
us

Updates
02 Jun 2005

Script turned into a function
Allow include parameters
Allow exclude parameters
Enable CLEAR command
Add test script
Improve help feature
remove recursive nature of created vars
remove temporary variables
use TMW help standards

03 Jun 2005

Script turned into a function
Allowed include parameters
Allowed exclude parameters
Enabled CLEAR command
Added test script
Improved help feature
removed recursive nature of created vars
removed temporary variables
used TMW help standards

Contact us