function theResult = whov(varargin) % whov -- WHOS + value. % w = whov (no argument) returns a WHOS structure % with an added "value" field for each item in % the caller's workspace. % whov(w) restores the values given in % struct "w" to the caller's workspace. % w = whov('matfilename') performs "whov" on % the contents of the given mat-file. % whov('matfilename', w) restores the values % in struct "w" to the given mat-file, using % the "-append" option of the "save" function. % This is tricky, since "save" has no way to % specify the name of an item separately from % its value. % Copyright (C) 1998 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 12-Aug-1998 19:07:07. % Updated 21-Sep-2001 10:06:45. % Note: we append "---" to all the names, to avoid % collisions when restoring values to a mat-file. if nargin < 1 % Save from caller. w___ = evalin('caller', 'whos'); if length(w___) > 0 w___(end).value = []; for i = 1:length(w___) w___(i).value = evalin('caller', w___(i).name); end end elseif nargin < 2 & ischar(varargin{1}) % Mat-file. matfile___ = varargin{1}; w___ = whos('-file', matfile___); if length(w___) > 0 v___ = load(matfile___); for i = 1:length(w___) w___(i).value = getfield(v___, w___(i).name); end end elseif nargin < 2 % Restore to caller. w___ = varargin{1}; for i___ = 1:length(w___) assignin('caller', w___(i___).name, w___(i___).value) end elseif nargin > 1 & ischar(varargin{1}) & isstruct(varargin{2}) % Matfile. matfile___ = varargin{1}; w___ = varargin{2}; for i___ = 1:length(w___) s___ = [w___(i___).name ' = w___(i___).value;']; eval(s___); save(matfile___, w___(i___).name, '-append') end end if nargout > 0 theResult = w___; end