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

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.14 $ $Date: 2000/06/01 04:17:58 $

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

if isempty(name), return, end

% Nothing but a row vector should be operated on.
[m,n] = size(name);
if (m > 1)
  error('Input cannot be a padded string matrix.');
end

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

% Look for PATH and FILENAME parts
if iswinpc
    ind = find(name == filesep | name == ':');
else
    ind = find(name == filesep);
end
if isempty(ind)
    path = '';
    fname = name;
else
    
    if iswinpc 
        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
    elseif  strncmp(comp,'MAC',3),
        path = name(1:ind(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
