From - Mon Aug 18 16:32:25 1997
Xref: ml.csiro.au comp.soft-sys.matlab:27833
Path: ml.csiro.au!newsroom.utas.edu.au!harbinger.cc.monash.edu.au!news.mel.connect.com.au!news.syd.connect.com.au!nsw.news.telstra.net!news.telstra.net!newsfeed.dacom.co.kr!europa.clark.net!205.252.116.205!howland.erols.net!infeed1.internetmci.com!newsfeed.internetmci.com!199.117.161.1!csn!nntp-xfer-1.csn.net!boulder!usenet
From: Driscoll Tobin A <tad@agnesi.Colorado.EDU>
Newsgroups: comp.soft-sys.matlab
Subject: New function: MULTLOAD.M for loading multiple files
Date: 15 Aug 1997 11:43:09 -0600
Organization: University of Colorado at Boulder
Lines: 161
Message-ID: <qi7bu2zmjf6.fsf@agnesi.Colorado.EDU>
NNTP-Posting-Host: agnesi.colorado.edu
Mime-Version: 1.0 (generated by tm-edit 7.106)
Content-Type: text/plain; charset=US-ASCII
NNTP-Posting-User: tad
X-Newsreader: Gnus v5.4.37/XEmacs 19.15

I sometimes run largish jobs in the background, saving results in
files like 'run01.mat', 'run02.mat', etc. Each file has the same
variable names in it, so when I want to compare runs, I have to LOAD
each data set and store it somehow. With cell arrays, this is easy to
do, if I know what variable names to store, but I decided to write a
general-purpose tool.

MULTLOAD accepts a list (or cell array, or char array) of
filenames. The variables in those files are loaded in succession, and
duplicates are stored in cell arrays. See the documentation for
details. 

Also, [N,V] = MULTLOAD(...) returns names and values without making
assignments in your workspace. This is a natural complement to the
behavior of WHO -FILE.

	-- Toby 

---------------------------------------------------------------------
 Tobin A. Driscoll         tad@colorado.edu
 Dept of Applied Math      http://amath.colorado.edu/appm/faculty/tad
 CB 526                    Phone: (303) 492-6974
 University of Colorado    Fax:   (303) 492-4066
 Boulder, CO 80309-0526
---------------------------------------------------------------------


function [Name,Values] = multload(varargin)
%MULTLOAD Load multiple files, accumulating duplicated values.
%   MULTLOAD(FILE1,FILE2,...) loads the files named FILE1, FILE2, ...,
%   in succession. If a variable name occurs multiple times in these
%   files, the value assigned to that name is a cell array whose
%   elements are all the values encountered. (The information about what
%   file each value came from is lost, but values appear in the order
%   they were read.)
%   
%   MULTLOAD(FILES), where FILES is a char array or cell array, is also
%   valid. 
%   
%   [NAME,VALUES] = MULTLOAD(...) makes no variable assignments in the
%   caller's workspace. Instead, NAME and VALUES are cell arrays, where
%   NAME{K} is a variable name and VALUES{K} is a single value or a cell
%   array of values for that name.
%   
%   Example:
%
%   >> who -file ex1
%   Your variables are:
%   t         x         y
%
%   >> who -file ex2
%   Your variables are:
%   x         y         
%   
%   >> multload('ex1','ex2')
%   >> x
%   x = 
%       [10]    [1]
%   >> t
%   t =
%        0
%   
%   See also WHO, LOAD.

%   Copyright (c) 1997 by Toby Driscoll (driscoll@na-net.ornl.gov). 
%   Version 1, 8/15/97

if nargin == 1
  filename = varargin{1};
else
  filename = varargin;
end

% If file names are given as char array, convert to cell array
if ischar(filename)
  filename = cellstr(filename);
end

% Initially no names and values exist
Name = '';
Values = {};

% Loop through file names
for j = 1:length(filename)
  % Load vars, separating names from values
  [name,value] = nvload(filename{j});
  if isempty(name)
    fprintf('Warning: File %s did not have recognizable variables.\n',...
	filename{j})
  else
    % Loop through variable names
    for k = 1:length(name)
      % Look for name in list of known names
      idx = strmatch(name{k},Name);
      if isempty(idx)
	% New variable name
	Name = strvcat(Name,name{k});	% add to list
	Values{size(Name,1)} = value{k}; % record value
      else
	% Previously known name
	if iscell(Values{idx})
	  % Already has multiple values, just tack on
	  Values{idx}{end+1} = value{k};	% add to values
	else
	  % This is the second value; convert to cell
	  Values{idx} = {Values{idx} value{k}};
	end
      end
    end
  end
end
	
% With no outputs, the caller wants the values assigned in the workspace
if nargout==0
  for k = 1:size(Name,1)
    assignin('caller',deblank(Name(k,:)),Values{k})
  end
  % Prevent assignment to 'ans'
  clear Name
else
  % Convert Name to a cell array (it was char for speed in STRMATCH)
  Name = cellstr(Name);
end
% End of multload
  

function [name,value] = nvload(filename)
%   This function loads a file, separating the names from the
%   values. This really ought to be built-in, IMO. WHO already does half
%   of the job.

% The X6qZ_ prefix is to avoid clashes with variable names in the file 

% Load names and the variables
X6qZ_name = who('-file',filename);
load(filename)

for X6qZ_j = 1:X6qZ_length(X6qZ_name)
  X6qZ_value{X6qZ_j} = X6qZ_eval(X6qZ_name{X6qZ_j});
end

% Don't need to worry about names any more
name = X6qZ_name;
value = X6qZ_value;
% End of nvload


%   The following functions are just "wrappers" with the X6qZ_ prefix,
%   to avoid careless users who might have reassigned those names in
%   their MAT file (say with 'eval' meaning 'eigenvalue').

function e = X6qZ_eval(expr)

e = evalin('caller',expr);


function n = X6qZ_length(var)

n = length(var);
				

