function [symind,symname] = mgetscope(symind,symname,catalog,ws,node)
%MGETSCOPE Handle parameter variable scoping.
%    MGETSCOPE looks up the SuperBlock hierarchy for the existence of a partition
%    name. If the partition is specified, MGETSCOPE look for each block parameter
%    variable under that partition. If no partition is specified, MGETSCOPE looks
%    for the next SuperBlock up the hierarchy. Finally, MGETSCOPE looks for the
%    variable in the current working partition.
%
% See also SLBUILD

% Initial coding by A S Bozin, 17-4-98
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.4 $ $Date: 2000/06/14 16:12:39 $

   %
   % Find all variable names
   %
   indmap = find(symind);
   if (~isempty(indmap))
      names = symname(indmap);
      np = size(names,1);
      %
      % Look for the explicit partition names
      %
      logind = zeros(1,np);
      for (k = 1:np)
	 pname = names{k};
	 ind = mfindstr(pname,1,'.');
	 if (ind)
	    logind(k) = 1;
	    partition = pname(1:ind-1);		   % Extract partition
	    parname = pname(ind+1:end);		   % and variable name
	    ind = find(strcmp(ws.names,partition));
	    if (isempty(ind))
	       symind(indmap(k)) = 0;		   % Parameter not found
	    else
	       ind = find(strcmp(ws.pvars{ind},parname));
	       if (isempty(ind))
		  symind(indmap(k)) = 0;	   % Parameter not found
	       end
	    end
	 end
      end
      %
      % Remove the parameters processed so far
      %
      remind = find(logind);
      if (~isempty(remind))
	 names(remind) = '';
	 indmap(remind) = [];
      end
      %
      % Find partitions for the rest of the variables
      %
      if (isempty(names))
	 eos = 1;
      else
	 np = size(names,1);
	 eos = 0;
	 logind = zeros(1,np);
      end
      while (~eos)				   % Browse the partitions
	 partition = catalog(node).partition;
	 while (isempty(partition))
	    node = catalog(node).caller;
	    partition = catalog(node).partition;
	 end
	 ind = find(strcmp(ws.names,partition));
	 if (~isempty(ind))
	    pvars = ws.pvars{ind};
	    parind = find(logind == 0);
	    for (k = parind)
	       ind = find(strcmp(pvars,names{k}));
	       if (~isempty(ind))
		  logind(k) = 1;		   % Variable found
		  symname{indmap(k)} = cat(2,partition,'.',names{k});
	       end
	    end
	 end
	 node = catalog(node).caller;
	 if (all(logind) | (node == 0))
	    eos = 1;
	 end
      end
      symind(indmap(find(logind == 0))) = 0;	   % Need numerical value
   end
   return					   % Last card of MGETSCOPE
