function multigraf(arg)
  
% MULTIGRAF enables putting up to six MATLAB figures into a single
%           figure.  To use it, simply enter MULTIGRAF at the MATLAB
%           prompt.  This will open a figure with six available slots.
%           Simply make a choice for each slot.  When you are satisfied,
%           click the Finish button.
%     
%           There are special buttons for DFIELD5 and PPLANE5 figures,
%           since these need special handling.

  if nargin == 0
    arg = 'initialize';
    ngraphs = 6;
  end
  
  switch arg
   case 'initialize'
    fsize = [560 705];
    left = [45,305,45,305,45,305];
    bot = [50,50,275,275,500,500];
    paperpos = [0.25,0.25,fsize/70];
    
    ud.left = left;
    ud.bot = bot;
    mgh = findobj('tag','MF');
    if ~isempty(mgh),delete(mgh);end
    newfig = figure('pos',[40,40,fsize],...
		    'paperposition',paperpos,...
		    'vis','off','tag','MF');
    dbutt(1) = uicontrol('style','push',...
			 'string',' Insert the current DFIELD Display here ');
    ext = get(dbutt(1),'extent');
    texth = 24;
    bw = ext(3);
    for jj = 2:ngraphs
      dbutt(jj) = uicontrol('style','push',...
			    'string',' Insert the current DFIELD Display here ');
      call = 'multigraf(''dinsert'')';
    end
    for j=1:ngraphs
      set(dbutt(j),'pos',[left(j),bot(j),bw,texth],...
		   'user',j,'call',call);
    end
    
    call = 'multigraf(''pinsert'')';
    for jj = 1:ngraphs
      pbutt(jj) = uicontrol('style','push',...
			    'string',' Insert a PPLANE graph here ',...
			    'pos',[left(jj),bot(jj)+texth,bw,texth],...
			    'user',jj,'call',call);
    end
    
    call = 'multigraf(''ginsert'')';
    for jj = 1:ngraphs
      gbutt(jj) = uicontrol('style','push',...
			    'string',' Insert a MATLAB graph here ',...
			    'pos',[left(jj),bot(jj)+2*texth,bw,texth],...
			    'user',jj,'call',call);
    end
    
    call = 'multigraf(''delete'')';
    for jj = 1:ngraphs
      delbutt(jj) = uicontrol('style','push',...
			      'string',' Delete ');
    end
    ext = get(delbutt(1),'extent');
    bwd = ext(3);
    call = 'multigraf(''delete'')';
    shift = 30;
    for j=1:ngraphs
      set(delbutt(j),'pos',[left(j)-shift,bot(j),bwd,texth],...
		     'user',j,'call',call);
    end
    
    call = 'multigraf(''finish'')';
    ud.finish = uicontrol('style','push',...
			  'string',' Finish ',...
			  'pos',[left(2),10,bw,texth],...
			  'call',call);
    
    call = 'multigraf(''refresh'')';
    ud.refresh = uicontrol('style','push',...
			   'string',' Start over ',...
			   'pos',[left(1),10,bw,texth],...
			   'call',call);
    set([newfig,dbutt,pbutt,gbutt,ud.finish,ud.refresh],'vis','on');
    set(delbutt,'vis','off')
    ud.dbutt = dbutt;
    ud.pbutt = pbutt;
    ud.gbutt = gbutt;
    ud.delbutt = delbutt;
    ud.axes = cell(1,ngraphs);
    set(newfig,'user',ud);
    
   case 'dinsert'
    hhsetup = get(0,'ShowHiddenHandles');
    set(0,'showhiddenhandles','on');
    figh = findobj('name','DFIELD5 Display');
    if isempty(figh)
      mess = 'There is no DFIELD5 Display figure.';
      title = 'No figures';
      msgbox(mess,title,'help');
      return
    end
    oldax = findobj(figh,'type','axes');
    newfig = gcf;
    ud = get(newfig,'user');
    newax = copyobj(oldax,newfig);
    ht = 165; width = 210;
    left = ud.left;
    bot = ud.bot;
    jj = get(gcbo,'user');
    oldunits = get(newax,'units');
    set(newax,'units','pix');
    set(newax,'pos',[left(jj),bot(jj),210,165]);
    set(newax,'units',oldunits);
    set([ud.dbutt(jj),ud.pbutt(jj),ud.gbutt(jj)],'vis','off');
    set(ud.delbutt(jj),'vis','on');
    ud.axes{jj} = newax;
    set(newfig,'user',ud);
    set(0,'showhiddenhandles',hhsetup);
    
   case 'ginsert'
    mgfig = gcf;
    hhsetup = get(0,'ShowHiddenHandles');
    set(0,'showhiddenhandles','on');
    fh = get(0,'children');
    fh(find(fh == mgfig)) = [];
    nf =0;
    nfh = zeros(0,1);
    for k = 1:length(fh)
      tag = get(fh(k),'tag');
      if ~strcmp(tag,'DFIELD5') & ~strcmp(tag,'PPLANE5')
	axhh = findobj(fh(k),'type','axes');
	if ~isempty(axhh)
	  nf = nf + 1;
	  name = get(fh(k),'name');
	  if strcmp(name,''),
	    name = ['Figure No. ',int2str(fh(k))];
	  end,
	  winstr{nf} = name;
	  nfh = [nfh;fh(k)];
	end
      end
    end
    if nf == 0
      mess = 'There are no suitable figures.';
      title = 'No figures';
      msgbox(mess,title,'help');
      return
    elseif nf == 1
      sel = 1;
    else
      [sel,ok] = listdlg('liststring',winstr,...
			 'selectionmode','single',...
			 'listsize',[270,100],...
			 'name','Figure selection',...
			 'promptstring','Select a figure:',...
			 'OKString','Select');
      if ~ok, return,end
    end
    figh = nfh(sel);
    oldax = findobj(figh,'type','axes');
    newfig = gcf;
    ud = get(newfig,'user');
    newax = copyobj(oldax,newfig);
    LLL = length(newax);
    ht = 165; width = 210;
    left = ud.left;
    bot = ud.bot;
    jj = get(gcbo,'user');
    oldunits = get(newax,'units');
    set(newax,'units','pix');
    set(newax,'pos',[left(jj),bot(jj),210,165]);
    if LLL == 1
      set(newax,'units',oldunits);
    else
      for k = 1:LLL
	set(newax(k),'units',oldunits{k});
      end
    end
    set([ud.dbutt(jj),ud.pbutt(jj),ud.gbutt(jj)],'vis','off');
    set(ud.delbutt(jj),'vis','on');
    ud.axes{jj} = newax;
    set(newfig,'user',ud);
    set(0,'showhiddenhandles',hhsetup);
    
   case 'pinsert'
    mgfig = gcf;
    hhsetup = get(0,'ShowHiddenHandles');
    set(0,'showhiddenhandles','on');
    fh = get(0,'children');
    fh(find(fh == mgfig)) = [];
    nf =0;
    nfh = zeros(0,1);
    for k = 1:length(fh)
      tag = get(fh(k),'tag');
      if strcmp(tag,'PPLANE5')
	axhh = findobj(fh(k),'type','axes');
	if ~isempty(axhh)
	  nf = nf + 1;
	  name = get(fh(k),'name');
	  if strcmp(name,''),
	    name = ['Figure No. ',int2str(fh(k))];
	  end,
	  winstr{nf} = name;
	  nfh = [nfh;fh(k)];
	end
      end
    end
    if nf == 0
      mess = 'There are no suitable figures.';
      title = 'No figures';
      msgbox(mess,title,'help');
      return
    elseif nf == 1
      sel = 1;
    else
      [sel,ok] = listdlg('liststring',winstr,...
			 'selectionmode','single',...
			 'listsize',[270,100],...
			 'name','Figure selection',...
			 'promptstring','Select a figure:',...
			 'OKString','Select');
      if ~ok, return,end
    end
    figh = nfh(sel);
    oldax = findobj(figh,'type','axes');
    raxh = findobj(oldax,'tag','rotaObj'); % Is rotate3d on?
    oldax = setdiff(oldax,raxh);
    L = length(oldax);
    if L>2;
      oldax = oldax([3;2]);
    end
    r = rand(1,3);
    newfig = gcf;
    ud = get(newfig,'user');
    left = ud.left;
    bot = ud.bot;
    newax = copyobj(oldax,newfig);
    ht = 165; width = 210;
    jj = get(gcbo,'user');
    set(newax,'units','pix');
    if L > 2
      campos = get(oldax(1),'cameraposition');
      camup = get(oldax(1),'cameraupvector');
      set(newax(1),'cameraposition',campos);
      camposmode = get(oldax(1),'camerapositionmode');
      camtarget = get(oldax(1),'cameratarget');
      camtargmode = get(oldax(1),'cameratargetmode');
      camupvectormode = get(oldax(1),'cameraupvectormode');
      set(newax(1),'camerapositionmode',camposmode,...
		   'cameraposition',campos,...
		   'cameratarget',camtarget,...
		   'cameratargetmode',camtargmode,...
		   'cameraupvectormode',camupvectormode,...
		   'cameraupvector',camup);    
    end
    set(newax(1),'pos',[left(jj),bot(jj),210,165]);
    set(newax(2),'pos',[left(jj),bot(jj)+165,210,30]);
    set(newax,'units','normal');
    set([ud.dbutt(jj),ud.pbutt(jj),ud.gbutt(jj)],'vis','off');
    set(ud.delbutt(jj),'vis','on');
    ud.axes{jj} = newax;
    set(newfig,'user',ud);
    set(0,'showhiddenhandles',hhsetup);
    
   case 'delete' 
    ud = get(gcf,'user');
    jj = get(gcbo,'user');
    delete(ud.axes{jj});
    ud.axes{jj} = [];
    set(gcf,'user',ud);
    set([ud.dbutt(jj),ud.pbutt(jj),ud.gbutt(jj)],'vis','on');
    set(ud.delbutt(jj),'vis','off');
    
   case 'finish'
    ud = get(gcf,'user');
    delete([ud.dbutt,ud.pbutt,ud.gbutt,ud.delbutt,ud.refresh]);
    delete(ud.finish);
    
   case 'refresh'
    ud = get(gcf,'user');
    axes = ud.axes;
    ngraphs = length(axes);
    axh = [];
    for jj = 1:ngraphs
      axh = [axh;ud.axes{jj}];
    end
    if ~isempty(axh),delete(axh);end
    set([ud.dbutt,ud.pbutt,ud.gbutt],'vis','on');
    set(ud.delbutt,'vis','off')
    ud.axes = cell(1,ngraphs);
    set(gcf,'user',ud);
    
   otherwise
    mess = 'MULTIGRAF does not need any input parameters.';
    title = 'Incorect input';
    msgbox(mess,title,'help');
    return
    
    
  end
