ToSavedWorkspace

버전 1.0.0.0 (3 KB) 작성자: Michael Robbins
Saves the workspace variables to the structure
다운로드 수: 2.9K
업데이트 날짜: 2005/6/3

라이선스 없음

편집자 메모: 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

인용 양식

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

MATLAB 릴리스 호환 정보
개발 환경: R13
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
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