function [ny,nu,ky,ku] = iduiiono(arg,info,type,win,noclick)
%IDUIIONO Handles everything related to input-output numbers
%      The argument ARG takes the following values
%	'old': With INFO being the data/model info matrix and
%	       TYPE being either 'dat' or 'mod', the function
%	       returns as NU and NY the number of inputs and outputs
%	       in the data/model. With WIN containing the physical
%	       input and output numbers, KU and KY are returned as
%	       the corresponding logical numbers in the representation
% 	'set': Sets all the submenus to 'Channel'.
%	       INFO may then contain (as row 6) the physical output
%	       numbers to be represented and (as row 7) the physiscal
%	       input numbers to be represented in the submenus	
%	       TYPE contains the plot window numbers for which to set
%	       the submenus (default 1 2 4 5 7 13 )
%	'unpack': returns as NY and NU the chosen channel in window
%	       number INFO
%	'update': Marks the correct channel

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.5 $  $Date: 1995/05/02 16:32:08 $

global  XIDplotw XIDcounters

if strcmp(arg,'old')
  if strcmp(type,'dat')
     adder=0;
  else
     adder=1;
  end

  ny=eval(deblank(info(2+adder,:)));
  nu=eval(deblank(info(5+adder,:)));
  if nargin<4
     ky=1;ku=1;
  else
     kudes=win(1);
     kydes=win(2);
     ky=find(eval(['[',deblank(info(6+adder,:)),']'])==kydes);
     if kudes>0
        ku=find(eval(['[',deblank(info(7+adder,:)),']'])==kudes);
     elseif kudes==0
        ku=0;
     else
        ku=-find(eval(['[',deblank(info(6+adder,:)),']'])==-kudes);
     end
     if length(ky)>1,ky=ky(1);end
     if length(ku)>1,ku=ku(1);end
  end

elseif strcmp(arg,'set')
%   iduistat('Modifying Channel menus...')
   if nargin<2, info=[];end
   if isempty(info)
         nu=XIDcounters(3);ny=XIDcounters(4);nuseq=1:nu;nyseq=1:ny;
   end
   if ~isempty(info)&isstr(info)
         nyseq=sort(eval(['[',deblank(info(6,:)),']']));ny=length(nyseq);
         nuseq=sort(eval(['[',deblank(info(7,:)),']']));nu=length(nuseq);
   elseif ~isempty(info)
         nu=info(1);ny=info(2);nuseq=1:nu;nyseq=1:ny;
         if length(info)>1+nu,
             nuseq=info(3:2+nu);
         end
         if length(info)>2+nu
             nyseq=info(3+nu:length(info));
         end


   end
   if nargin<3
      win=[1 2 3 4 5 6 7 13];
   else
      win=type;
   end

   for kwin=win
     if ~isempty(iduiwok(kwin))%iduigetp(kwin,'check')
       skwin=int2str(kwin);
       eval('oldsubm=get(XIDplotw(kwin,3),''children'');','oldsubm=[];')
       eval('usd=get(XIDplotw(kwin,3),''Userdata'');','usd=[];')
       if isempty(usd),usd=[-1,-1];end
       delete(idnonzer(oldsubm))
       oldok=0;        
        oldstr=[];%       oldstr=get(XIDplotw(kwin,1),'name'); % FIXA
       if (nu==0&any(kwin==[6,1,13,14,15]))|kwin==3|kwin==7
          for ky=nyseq
           if usd(1)==ky&usd(2)==0,oldok=1;che=1;else che=0;end
           sky=int2str(ky);sku=int2str(0);
           mu=uimenu(XIDplotw(kwin,3),'Label',['y',int2str(ky)],...
             'callback',['iduipoin(1);',...
             'iduiiono(''update'',',sky,',',sku,',',skwin,');iduipoin(2);']);
           if che,set(mu,'checked','on');end
           if ky==nyseq(1),muc=mu;end
          end   
          if ~oldok,
             str=[': y',int2str(nyseq(1))];usd=[nyseq(1),0];
             set(XIDplotw(kwin,3),'Userdata',usd);
             set(muc,'checked','on')
             figna=get(XIDplotw(kwin,1),'name');
             colno=find(figna==':');
             if ~isempty(colno)
                figna=figna(1:colno(1)-1);
             end
             set(XIDplotw(kwin,1),'name',[figna,str]);
          end
       elseif ~(nu==0&kwin==2)
          if (nu+ny)*ny>9,submen=1;else submen=0;end
          if kwin==4|kwin==5,  nuseq=1:nu+ny;end 
          for ku=nuseq
           if ku>nu&(kwin==4|kwin==5), 
              su=['e',int2str(ku-nu),'->'];sku=int2str(nu-ku);
           else
              su=['u',int2str(ku),'->'];sku=int2str(ku);
           end
           if submen
              dum=uimenu(XIDplotw(kwin,3),'Label',su);
           end
           for ky=nyseq           
              sky=int2str(ky);
              if submen, far=dum;else far=XIDplotw(kwin,3);end
              mu=uimenu(far,'Label',[su,'y',int2str(ky)],...
              'callback',['iduipoin(1);',...
              'iduiiono(''update'',',sky,',',sku,',',skwin,');iduipoin(2);']);
              if usd==[ky,eval(sku)];oldok=1;set(mu,'checked','on');end
              if ky==nyseq(1)&ku==nuseq(1),muc=mu;end
           end
          end  % for ku
          if ~oldok,
            if nu==0
               str=[': e',int2str(nyseq(1)),'->y',int2str(nyseq(1))];
               usd=[nyseq(1) -nyseq(1)];
            else 
               str=[': u',int2str(nuseq(1)),'->y',int2str(nyseq(1))];
               usd=[nyseq(1),nuseq(1)];
            end
            set(XIDplotw(kwin,3),'Userdata',usd);
            set(muc,'checked','on');
            figna=get(XIDplotw(kwin,1),'name');       
            colno=find(figna==':');
            if ~isempty(colno)
                figna=figna(1:colno(1)-1);
            end
            set(XIDplotw(kwin,1),'name',[figna,str]);
          end % if not OK
        end   % if nu==0
      end % if iduigetp
      end    % for kwin
%      iduistat('')
elseif strcmp(arg,'unpack')
  nn=get(XIDplotw(info,3),'userdata');
  if isempty(nn),ny=1;nu=1;return,end % This is just a fix for freq resp
  ny=nn(1);
  if length(nn)>1,nu=nn(2);end
elseif strcmp(arg,'update')
  if nargin<5
    curobj=get(gcf,'currentmenu');
  else
    sku=int2str(type);sky=int2str(info);skwin=int2str(win);
    cbf=['iduipoin(1);',...
             'iduiiono(''update'',',sky,',',sku,',',skwin,');iduipoin(2);'];
    curobj=findobj(XIDplotw(win,1),'callback',cbf);
  end
  if nargin<4,win=iduigetp(gcf);end
   usd=get(XIDplotw(win,3),'UserData');
   if usd(1)==info&usd(2)==type,
      return,end
   iduistat('Changing channel...')
  chanch=get(XIDplotw(win,3),'children');
  choff=chanch(:);
  for kc=chanch(:)'
     choff=[choff;get(kc,'children')];
  end
  set(choff,'checked','off')
  set(curobj,'checked','on')
  ky=info;ku=type;
  if ku==0
     label=[': y',int2str(ky)];
  else
     if ku<0,
        label=[': e',int2str(-ku),'->'];
     else
        label=[': u',int2str(ku),'->'];
     end
     label=[label,'y',int2str(ky)];
   end
   set(XIDplotw(win,3),'Userdata',[info,type]);
   figna=get(XIDplotw(win,1),'name');
   colno=find(figna==':');if ~isempty(colno),figna=figna(1:colno(1)-1);end
   set(XIDplotw(win,1),'name',[figna,label]);

   iduiclpw(win,1);
   iduistat('')
end


