function out = open(name)
%OPEN	 Open files by extension.
%   OPEN NAME where NAME must contain a string, does different things
%   depending on the type of the object named by that string:
%
%   Type         Action
%   ----         ------
%   variable      open named array in Array Editor
%   .mat  file    open MAT file; store variables in a structure in the workspace
%   .fig  file    open figure in Handle Graphics
%   .m    file    open M-file in M-file Editor
%   .mdl  file    open model in SIMULINK
%   .p    file    open the matching M-file if there is one
%   .html file    open HTML document in Help Browser
%
%   OPEN works similar to LOAD in the way it searches for files.
%
%     If NAME exists on MATLAB path, open file returned by WHICH.
%
%     If NAME exists on file system, open file named NAME.
%
%   Examples:
%
%     open('handel')           error if handel.mdl, handel.m, and handel
%                              are not on the path.
%
%     open('handel.mat')       error if handle.mat is not on path.
%
%     open('d:\temp\data.mat') error if data.mat is not in d:\temp.
%
%
%   OPEN is user-extensible.  To open a file with the extension ".XXX",
%   OPEN calls the helper function OPENXXX, that is, a function
%   named 'OPEN', with the file extension appended.
%
%   For example,
%      open('foo.m')       calls openm('foo.m')
%      open foo.m          calls openm('foo.m')
%      open myfigure.fig   calls openfig('myfigure.fig')
%
%   You can create your own OPENXXX functions to change the way standard
%   file types are handled, or to set up handlers for new file types.
%   OPEN will call whatever OPENXXX function it finds on the path.
%
%   Special cases:
%       for workspace variables, OPEN calls OPENVAR
%       for image files, OPEN calls OPENIM
%
%   If there is no matching helper function found, OPEN calls OPENOTHER.
%
%   See also SAVEAS, WHICH, LOAD, UIOPEN, UILOAD, PATH.
%

%   Chris Portal 1-23-98
%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.26 $  $Date: 2000/06/21 09:53:02 $

error(nargchk(1,1,nargin));

[m n]=size(name);

if iscell(name) | ~ischar(name) | m~=1,
    error('NAME must contain a single string.');
end

% try to qualify name if we can't find it - good for openvar
if exist(name) == 0
    fullpath = evalin('caller', ['which(''' strrep(name, '''','''''') ''')']);
else
    fullpath = which(name);
end

if isempty(fullpath)
    % try exist if which can't find it
    if exist(name) == 2
        fullpath = name;
    end
end

if isempty(fullpath)
    % which did not find it and fopen didn't find it either
    error(sprintf('File ''%s'' not found.',name))
end

% get the path and ext
path=fileparts(fullpath);

if isempty(path)
    % handle special cases for variables and built-ins
    switch fullpath
     case 'variable'
      evalin('caller', ['openvar(''' name ''');']);
     case 'built-in'
      error(sprintf('Can''t open the built-in function ''%s''.',name));
     otherwise
      error(['Internal error: unrecognized result from WHICH: ' fullpath])
    end
else
    % let finfo decide the filetype
    [fileType, openAction] = finfo(fullpath);
    if isempty(openAction)
        openAction = 'openother';
    end
    
    try
        % if opening a mat file be sure to fetch output args
        if isequal(openAction, 'openmat') | nargout
            out = feval(openAction,fullpath);
        else
            feval(openAction,fullpath);
        end
    catch
        % we only want the message from lasterr, not the stack trace
        le = lasterr;
        euIndex = findstr(le, 'Error using');
        if ~isempty(euIndex)
            le = le(euIndex(end):end);
            nlIndex = findstr(le,sprintf('\n'));
            if ~isempty(nlIndex)
                le = le(nlIndex(1) + 1 : end);
                while ~isempty(le) & length(le) > 1 & strcmp(le(end), sprintf('\n'))
                    le = le(1:end - 1);
                end
            end
        end
        error(le);
    end
end 
