function uiopen(type,direct)
%UIOPEN Present file selection dialog with appropriate file filters.
%
% UIOPEN always presents a file selection dialog.  The user can either
% choose a file to open or click cancel.  No further action is taken
% if the user hits cancel.  Otherwise the OPEN command is evaluated in the
% base workspace with the user specified file name.
%
% These are the file filters presented using UIOPEN.
%
% Input argument       Filter List
% <no input args>      *.m, *.fig, *.mat,
%                      *.mdl               (if Simulink is installed),
%                      *.cdr               (if Stateflow is installed),
%                      *.rtw, *.tmf, *.tlc, *.c, *.h, *.ads, *.adb
%                                          (if Real-Time Workshop is installed),
%                      *.*
% MATLAB               <same as no input args>
% LOAD                 *.mat, *.*
% FIGURE               *.fig, *.*
% SIMULINK             *.mdl, *.*
% EDITOR               *.m,
%                      *.mdl               (if Simulink is installed),
%                      *.cdr               (if Stateflow is installed),
%                      *.rtw, *.tmf, *.tlc, *.c, *.h, *.ads, *.adb
%                                          (if Real-Time Workshop is installed),
%                      *.*
%
% If the input argument is unrecognzied, it is treated as a file filter
% and passed directly to the UIGETFILE command.
%
% See also UIGETFILE UIPUTFILE OPEN UILOAD UIIMPORT 

% Copyright 1984-2000 The MathWorks, Inc. 
% $Revision: 1.15 $  $Date: 2000/07/06 19:45:33 $

if nargin == 0
    type = 'matlab';
end

allML = {'*.m;*.fig;*.mat', 'All MATLAB Files'};
if exist('simulink','builtin')
    allML(1)=strcat(allML(1), ';*.mdl');
end
if exist('stateflow')
    allML(1)=strcat(allML(1),';*.cdr');
end
if exist('rtwbuild')
    allML(1)=strcat(allML(1),';*.rtw;*.tmf;*.tlc;*.c;*.h;*.ads;*.adb');
end

switch(lower(type))
 case 'matlab'
     filterList = [
         allML; ...
         {'*.m',   'M-files (*.m)'; ...
          '*.fig', 'Figures (*.fig)'; ...
          '*.mat', 'MAT-files (*.mat)'}
      ];
      if exist('simulink','builtin')
          [rows,columns]=size(filterList);
          filterList {rows+1, 1}='*.mdl';
          filterList {rows+1, 2}='Models (*.mdl)';
      end
      if exist('stateflow')
          [rows,columns]=size(filterList);
          filterList {rows+1, 1}='*.cdr';
          filterList {rows+1, 2}='Stateflow files (*.cdr)';
      end
      if exist ('rtwbuild')
          [rows,columns]=size(filterList);
          filterList {rows+1, 1}='*.rtw;*.tmf;*.tlc;*.c;*.h;*.adb;*.ads';
          filterList {rows+1, 2}='Real-Time Workshop files (*.rtw,*.tmf,*.tlc,*.c,*.h,*.adb,*.ads)';
      end
      [rows,columns]=size(filterList);
      filterList {rows+1, 1}='*.*';
      filterList {rows+1, 2}='All Files (*.*)';
  case 'load'
      filterList = [
          {'*.mat', 'MAT-files (*.mat)'}; ...
          allML; ...
          {'*.*',   'All Files (*.*)'}
          ];
 case 'figure'
      filterList = [
          {'*.fig', 'Figures (*.fig)'}; ...
          allML; ...
          {'*.*',   'All Files (*.*)'}
          ];
 case 'simulink'
      filterList = [
          {'*.mdl', 'Model (*.mdl)'}; ...
          allML; ...
          {'*.*',   'All Files (*.*)'}
          ];
 case 'editor'
     MLText = {'*.m', 'All MATLAB Files'};
     if exist('simulink','builtin')
         MLText(1)=strcat(MLText(1),';*.mdl');
     end
     if exist('stateflow')
         MLText(1)=strcat(MLText(1),';*.cdr');
     end
     if exist('rtwbuild')
         MLText(1)=strcat(MLText(1),';*.rtw;*.tmf;*.tlc;*.c;*.h;*.ads;*.adb');
     end

     filterList = [
          MLText;...
          {'*.*',   'All Files (*.*)'}
          ];
 otherwise
  filterList = type;
end

if nargin < 2
    direct = 0;
end

if direct
    fn = type;
else
    [fn,pn] = uigetfile(filterList,'Open');
    if all(pn == 0) | all(fn == 0)
        return;
    end
    fn = fullfile(pn,fn);
end

try
    
    ans = [];

    if strcmp(lower(type),'editor') % route open requests from editor back to editor
        edit(fn);
    else
        open(fn);
    end

    if ~isempty(ans)
        % this was likely a load, shove vars into calling wksp
        if isstruct(ans)
            names = fieldnames(ans);
            for i = 1:length(names)
                assignin('caller',names{i}, getfield(ans,names{i}));
            end
        else
            assignin('caller','ans',ans);
        end
        disp('Variables created in current workspace.')

        % force workspace browser to refresh if it's there
        try, workspace('-refresh'), end
    end
catch
    errordlg(lasterr);
end
