% function matout = xtracti(mat,ivindex)
%
%   If MAT is a VARYING matrix, and IVINDEX is a 
%   positive integer, with IVINDEX <= LENGTH(GETIV(MAT)),
%   then MATOUT is the matrix associated with
%   the IVINDEX'th INDEPENDENT VARIABLE's value.
%   This applies to the top level independent variable,
%   in the case of nested IV's.
%   If MAT is a CONSTANT or SYSTEM matrix, and INDVINDX is a
%   positive integer, then MATOUT is equal to MAT.
%
%   See also: GETIV, MINFO, SCLIV, SEL, VAR2CON, and XTRACT.

%   modified for NESTED VARYING MATRICES, ivindex should
%	be a vector reflecting desired iv value at each level,
%	as usual, from slowest to fastest.

function matout = xtracti(mat,indvnum)
  err = 0;
  if nargin < 2
    disp('usage: matout = xtracti(mat,ivindex)')
    return
  end
  if indvnum <= 0 | floor(indvnum) ~= ceil(indvnum)
    error('INDEPENDENT VARIABLE index should be a positive integer')
    return
  end
  matout = mat;
  [mtype,mrows,mcols,mnum] = minfo(matout);
  if strcmp(mtype,'vary')
    if mnum >= indvnum(1)
      matout = matout((indvnum-1)*mrows+1:indvnum*mrows,1:mcols);
      if length(indvnum) > 1
        indvnum = indvnum(2:length(indvnum));
        matout = xtracti(matout,indvnum);
      end
    else
      error('INDEPENDENT VARIABLE index too large for VARYING data')
      return
    end
  else
    matout = mat;
  end
%
% Copyright MUSYN INC 1991,  All Rights Reserved
