function c = convertCell2Struct(chain)
% convertCell2Struct

% Author: D. Orofino
% Copyright 1984-2000 The MathWorks, Inc. 
% $Revision: 1.3 $ $Date: 2000/06/01 03:16:20 $

c={};
temp={};

for i=1:length(chain),
   if length(chain{i})==3,    % skip single-input case
      x1_idx  = chain{i}(1);  % x1 matrix index
      x2_idx  = chain{i}(2);  % x2 matrix index
      x12_idx = chain{i}(3);  % x12 result index
      
      % Get references to input matrices
      % Use letter variable names, or
      % structure entry from temp storage:
      p.Lvar=getMatrixRef(x1_idx, temp);
      p.Rvar=getMatrixRef(x2_idx, temp);
      
      if x12_idx == 0,
         % output area specified for destination
         % we are done:
         c=p;
      else
         % A temporary storage area has been specified
         tempIdx = -x12_idx; % create positive temp index
         temp{tempIdx} = p;
      end
   end
end


% ----------------------------------------------------
function x_ref = getMatrixRef(x_idx, temp_refs)
% getMatrixRef
%
% Positive indices are taken to be input matrices,
% negative indices are temp matrices.
% The 0'th index is the output matrix, and is not available
% for use in intermediate results (at least in the enumerated
% specifications).

%  If index is negative, return size-vector from temp_sizes
%  Otherwise, return size-vector from matrix_sizes
if x_idx<0,
   x_ref = temp_refs{-x_idx};
elseif x_idx>0,
   x_ref = char('A'-1+x_idx);
else
   error(['Input index to matrix multiplier pair cannot be ' ...
         'specified as the output matrix (index 0).']);
end

% [EOF] convertCell2Struct.m
