ToSavedWorkspace

Saves the workspace variables to the structure
2.9K Downloads
Updated 3 Jun 2005

No License

Editor's Note: This file was a File Exchange Pick of the Week

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

Cite As

Michael Robbins (2024). ToSavedWorkspace (https://www.mathworks.com/matlabcentral/fileexchange/7642-tosavedworkspace), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Workspace Variables and MAT-Files in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0

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