function [path, fname, extension,version] = fileparts(name)
%FILEPARTS Filename parts.
%   [PATH,NAME,EXT,VER] = FILEPARTS(FILE) returns the path, filename, 
%   extension and version for the specified file.  VER will be non-empty
%   only on VMS.  FILEPARTS is platform dependent.
%
%   You can reconstruct the file from the parts using
%      fullfile(path,[name ext ver])
%   
%   See also FULLFILE, PATHSEP, FILESEP.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.9 $ $Date: 1998/11/20 02:23:50 $

path = '';
fname = '';
extension = '';
version = '';

if isempty(name), return, end

% Convert all / to \ on PC
if strncmp(computer,'PC',2)
    orig_name = name;
    name = strrep(name,'/','\');
end

% Look for PATH and FILENAME parts
if isvms
    ind = find(name == ']');
    if isempty(ind)
        ind = find(name == ':');
    end
elseif strncmp(computer,'PC',2)
    ind = find(name == filesep | name == ':');
else
    ind = find(name == filesep);
end
if isempty(ind)
    path = '';
    fname = name;
else
    if isvms | strncmp(computer,'MAC',3),
        path = name(1:ind(end)); 
    elseif strncmp(computer,'PC',2) 
        if name(ind(end)) == ':'
            path = orig_name(1:ind(end));
        else 
            path = orig_name(1:ind(end)-1);
        end
        if ~isempty(path) & path(end)==':'
            path = [path '\'];
        end
    else 
        path = name(1:ind(end)-1); 
    end
    fname = name(ind(end)+1:end);
end

if isempty(fname), return, end

% Look for EXTENSION part
ind = max(find(fname == '.'));

if isempty(ind)
    return
else
    extension = fname(ind:end);
    fname(ind:end) = [];

    % Make sure name does not have two consecutive dots - m-file's won't reload
    % if strcmp(fname(end), '.') & strcmp(extension(1), '.')
    %    error( [ 'Invalid filename: ' name ] );
    % end
end
if ~isvms | isempty(extension), return, end

% Look for VERSION part
ind = find(extension == ';');
if ~isempty(ind)
    version = extension(ind:end);
    extension(ind:end) = [];
end
