Class for working with filesystem paths in MATLAB with short and readable code.
Examples
Requirements
Installation
Reference
>> file = Path("C:\projects") \ "portal_gun.mdl"
Path("C:\projects\portal_gun.mdl")
>> file.parent
Path("C:\projects")
>> file.setExtension(".pew")
Path("C:\projects\portal_gun.pew")
>> Path("C:/stuff/../moreStuff\\//") \ "\subStuff//" / "./file.txt"
Path("C:\moreStuff\subStuff\file.txt")
>> sounds = Path("cat_sounds") / ["meow", "prr", "hsss"] + ".mp3"
Path("cat_sounds\meow.mp3")
Path("cat_sounds\prr.mp3")
Path("cat_sounds\hsss.mp3")
>> sounds.copyToDir("dog_triggers")
>> files = Path("C:\Windows").listFiles.where("Stem", "*Virus*").delete
Path.here.cd
MATLAB R2019b or newer
Download or clone this repository and add it to your MATLAB search path.
Path(arg1, arg2, ...)
Supports string
, char
or Path
vectors or cell
vectors of said types.
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 |