function midprefs(path)
%MIDPREFS Choose directory for idprefs.mat, the ident start-up info file.
%
%	midprefs  or midprefs(PATH)
%
% 	The Graphical User Interface to the System Identification toolbox
%	uses a file idprefs.mat to store start-up information, like preferred
%	window sizes and locations and most recently used session files.
%	By default this file is stored in the same directory as startup.m,
%	if this .m file exists.
%
%	To select another directory, use the function MIDPREFS. Simply type
%	the function name and follow the instructions, or give the directory
%	as an argument. Include all directory delimiters.
%
%	Example (PC): midprefs('c:\matlab\toolbox\local\')

%	L. Ljung 1-1-95
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%       $Revision: 1.3 $  $Date: 1995/05/10 21:36:59 $

global XIDlaypath XIDsessions XIDlayout

if exist('idprefs.mat')
   load idprefs.mat
else
   XIDlaypath=[];
   if ~exist('XIDsessions'), XIDsessions=[];end   
   if ~exist('XIDlayout'), XIDlayout=[];end
end
XIDpathold=XIDlaypath;
if nargin==1, 
  XIDlaypath=path;
else
  text1='You have currently no file idprefs.mat on your MATLABPATH.';
  text2=['idprefs.mat currently stored in ',XIDpathold.'.'];
  text3=str2mat(['Use the MATLAB file finder to select a directory',...
      ' where to put this file.'],...
      'The directory should be on your regular MATLABPATH.','',...
      'Press Cancel in the file finder to abort.','',...
      'Press Return to open the file finder.','');
  if isempty(XIDpathold)
    disp(str2mat(text1,text3))
  else
    disp(str2mat(text2,text3))
  end
  pause
  [dum,XIDlaypath]=uiputfile('idprefs.mat','Directory for start-up file');
  if XIDlaypath==0,return;end
end
err=0;
eval(['save ',XIDlaypath,...
         'idprefs.mat XIDlaypath XIDsessions XIDlayout'],'err=1;')
if err,
       error('File could not be written.')
end
test=exist('idprefs.mat');if ~test, err=1;end      
if err,
      eval(['delete ',XIDlaypath,'idprefs.mat'],'')
      error('The chosen directory is not on your MATLABPATH.')
end
disp(str2mat('',['Ident start-up info stored in ',XIDlaypath,'idprefs.mat.'],''))
del=0;
if ~isempty(XIDpathold)
    eval(['delete ',XIDpathold,'idprefs.mat'],''),del=1;
    disp(str2mat(['Old file ',XIDpathold,'idprefs.mat deleted.'],''))
end
err=0;test=exist('idprefs.mat');if ~test, err=1;end      
if err,
      disp(str2mat(['The saved file ',XIDlaypath,...
         'idprefs.mat is not on your MATLABPATH.'],'or not correctly named.'))
      if del,
        disp(str2mat('','In order not to lose the old information in',...
       [XIDlaypath,'idprefs.mat, you should move this file to a location'],...
     'on your MATLABPATH (giving it the name idprefs.mat) and then invoke',...
           'midprefs again.'))
      end
end

