Class for representing filesystem paths in MATLAB and solving path-related problems with short and readable code.
Features
Examples
Installation
Reference
- Get and set path name, parent, root, stem and extension
- Filter paths by extension, name, etc. using wildcards
- List files recursively
- Handle lists of paths
- Clean and resolve paths
- Build absolute and relative paths
- Create, copy, move, delete files and directories
- Get directory of currently executing MATLAB file
>> file = Path("C:\data") \ "model.dat"
Path("C:\data\model.dat")
>> file.parent
Path("C:\data")
>> file.stem
"model"
>> file.extension
".dat"
>> personalFolders = Path("Astronauts") / ["Arthur", "Trillian", "Zaphod"]
Path("Astronauts\Arthur")
Path("Astronauts\Trillian")
Path("Astronauts\Zaphod")
>> personalFolders.join("DONT_PANIC.txt").createEmptyFile;
>> files = Path("Sketchy Folder").listDeepFiles
Path("Sketchy Folder\DeleteStuffVirus.exe")
Path("Sketchy Folder\System32\nastyWorm.dll")
Path("Sketchy Folder\dark_corner\half_a_sandwich.dat")
Path("Sketchy Folder\WormholeResearch.pdf")
>> files.where("Stem", ["*Virus*", "*Worm*"], "ExtensionNot", ".pdf").moveToDir("D:\Quarantine");
scriptFile = Path.this
Path("C:/projects/SpaceCatapult/simulate.m")
scriptDir = Path.here
Path("C:/projects/SpaceCatapult")
scriptDir.cd;
Download or clone this repository and add it to your MATLAB search path. Requires R2019b or newer.
Create Path
objects by calling Path(...)
with one or multiple arguments of type string
vector, char
vector, cell
of string
or char
vectors.
Method | Return type | Description |
---|---|---|
string |
string |
Convert to string |
char |
char |
Convert to char array |
cellstr |
cell |
Convert to cell of char arrays |
Method | Return type | Description |
---|---|---|
name |
Path |
File or folder name without directory |
parent |
Path |
Parent directory |
root |
Path |
First directory element of absolute paths |
stem |
string |
File name without extension |
extension |
string |
File extension |
parts |
string |
Split path into list comprising root, folders and name |
strlength |
double |
Number of characters in the path string |
absolute |
Path |
Absolute path assuming the current working directory as reference |
relative |
Path |
Path relative to reference directory |
is |
logical |
Whether properties match patterns |
isAbsolute |
logical |
Whether path is absolute |
isRelative |
logical |
Whether path is relative |
Method | Return type | Description |
---|---|---|
/ , \ , join |
Path |
Join paths |
+ , addSuffix |
Path |
Add string to the end of the path |
setName |
Path |
Set file or folder name without directory |
setParent |
Path |
Set parent directory |
setRoot |
Path |
Set first directory element |
setStem |
Path |
Set file name without extension |
setExtension |
Path |
Set file extension |
addStemSuffix |
Path |
Add string to the end of the file stem |
regexprep |
Path |
Wrapper for built-in regexprep |
tempFileName |
Path |
Append random unique file name |
Method | Return type | Description |
---|---|---|
== , eq |
logical |
Whether path strings are equal |
~= , ne |
logical |
Whether path strings are unequal |
where |
Path |
Select paths where properties match patterns |
Method | Return type | Description |
---|---|---|
exists |
logical |
Whether path exists in filesystem |
isFile |
logical |
Whether path is an existing file |
isDir |
logical |
Whether path is an existing directory |
mustExist |
- | Raise error if path does not exist |
mustBeFile |
- | Raise error if path is not an existing file |
mustBeDir |
- | Raise error if path is not an existing directory |
modifiedDate |
datetime |
Date and time of last modification |
bytes |
- | File size in bytes |
mkdir |
- | Create directory if it does not already exist |
cd |
Path |
Wrapper for built-in cd |
createEmptyFile |
- | Create an empty file |
delete |
- | Delete files and directories. Remove directories recursively with optional argument 's' . |
fopen |
[double, char] |
Wrapper for built-in fopen |
open |
[double, onCleanup] |
Open file and return file ID and onCleanup object, which closes the file on destruction. Create parent directory if necessary. Raise error on failure. |
readText |
string |
Read text file |
writeText |
- | Write text file |
copy |
- | Copy to new path |
copyToDir |
- | Copy into target directory preserving the original name |
move |
- | Move to new path (rename) |
moveToDir |
- | Move into target directory preserving the original name |
listFiles |
Path |
List file paths in directory |
listDeepFiles |
Path |
List files paths in directory and all its subdirectories |
listFolders |
Path |
List directories in directory |
listDeepFolders |
Path |
List directories and subdirectories in directory |
Method | Return type | Description |
---|---|---|
isEmpty |
logical |
Check if array is empty |
count |
double |
Number of elements |
sort |
[Path, double] |
Sort by path string |
unique_ |
[Path, double, double] |
Wrapper for built-in unique |
deal |
[Path, Path, ...] |
Distribute array objects among output arguments |
Method | Return type | Description |
---|---|---|
Path.current |
Path |
Current working directory; wrapper for built-in pwd |
Path.home |
Path |
User home directory |
Path.tempDir |
Path |
Temporary directory; wrapper for built-in tempdir |
Path.tempFile |
Path |
Random unique file in temporary directory; wrapper for built-in tempname |
Path.matlab |
Path |
MATLAB install directory; wrapper for built-in matlabroot |
Path.searchPath |
Path |
Folders on MATLAB search path; wrapper for built-in path |
Path.userPath |
Path |
MATLAB user directory; wrapper for built-in userpath |
Path.ofMatlabFile |
Path |
Path of MATLAB file on the MATLAB search path |
Path.this |
Path |
Path of MATLAB file executing this method |
Path.here |
Path |
Directory of MATLAB file executing this method |
Path.empty |
Path |
Empty object array |
Method | Return type | Description |
---|---|---|
disp |
- | Display in console |
help |
- | Open documentation web page |