Code covered by the BSD License  

Highlights from
filepathparts

Be the first to rate this file! 0 Downloads (last 30 days) File Size: 1.92 KB File ID: #15474

filepathparts

by DS

 

02 Jul 2007 (Updated 04 Jul 2007)

Returns cell array of strings containing individual directories from a specified file path.

| Watch this File

File Information
Description

Simple function that complements the built-in functions FULLFILE and FILEPARTS. This function parses the pathstr output of fileparts and returns a cell array of strings containing the individual directories in the specified filepath string, given as input.

Example:

directory ='C:\Dir1\Dir2\Dir3\DirN';
filename = 'Filename.ext';
pandf = fullfile(directory,filename);
filepathparts(pandf)

ans = 'DriveLetter:'
      'Dir1'
      'Dir2'
      'Dir3'
      'DirN'

Algorithm*:
-----------
C = strread(pathstr,'%s','delimiter',...
            strrep(filesep,'\','\\'));

*suggested by Jos x@y.z

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
02 Jul 2007 Jos x@y.z

Why not use:
C = strread(FullPathName, '%s','delimiter',strrep(filesep,'\','\\'))

(the strrep to needed on PC platforms)

Why is there a '\' in the last cell?

02 Jul 2007 D S

Jos - I agree, your solution is much prettier. The extra '\' is an off-by-one index error. I've resubmitted the m-file, which is now based on your one-line algorithm. Thanks for the help ;)

14 Sep 2009 julien diener

what about:

C = regexp( FullPathName, '[^\\/]+', 'match');

04 Oct 2009 DS

Julien - C = regexp( FullPathName, '[^\\/]+', 'match'); should work pretty well in most cases, but about Unix systems where backslash is a valid character to use in a filename?

Please login to add a comment or rating.
Updates
03 Jul 2007

Changed the ugly STRFIND-based algorithm for the one-line solution suggested by Jos x@y.z.
Added uigetfile behavior for no input case. Added check to cope with path or path+filename input.

04 Jul 2007

Fixed the example in the m-file help text.

Tag Activity for this File
Tag Applied By Date/Time
strings DS 22 Oct 2008 09:18:02
path parse separate directory DS 22 Oct 2008 09:18:02

Contact us at files@mathworks.com