Path Class

Version 2.2.1 (17,6 KB) von Martin Koch
Class for handling file system paths with short and readable code
141 Downloads
Aktualisiert 26. Jul 2024

Path

Class for working with filesystem paths in MATLAB with short and readable code.

Examples
Requirements
Installation
Reference

View Path on File Exchange

Examples

Create

>> file = Path("C:\projects") \ "portal_gun.mdl"

   Path("C:\projects\portal_gun.mdl")

Properties

>> file.parent

    Path("C:\projects")

>> file.setExtension(".pew")

    Path("C:\projects\portal_gun.pew")

Clean

>> Path("C:/stuff/../moreStuff\\//") \ "\subStuff//" / "./file.txt"

    Path("C:\moreStuff\subStuff\file.txt")

Arrays

>> 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")

Delete files matching criteria

>> files = Path("C:\Windows").listFiles.where("Stem", "*Virus*").delete

Enter directory of executing file

Path.here.cd

Requirements

MATLAB R2019b or newer

Installation

Download or clone this repository and add it to your MATLAB search path.

Reference

Constructor

Path(arg1, arg2, ...)

Supports string, char or Path vectors or cell vectors of said types.

Type conversions

<markdown-accessiblity-table><table class="readme_table"> <thead> <tr> <th class="readme_th">Method</th> <th class="readme_th">Return type</th> <th class="readme_th">Description</th> </tr> </thead> <tbody> <tr> <td class="readme_td"><code>string</code></td> <td class="readme_td"><code>string </code></td> <td class="readme_td">Convert to string</td> </tr> <tr> <td class="readme_td"><code>char</code></td> <td class="readme_td"><code>char </code></td> <td class="readme_td">Convert to char array</td> </tr> <tr> <td class="readme_td"><code>cellstr</code></td> <td class="readme_td"><code>cell </code></td> <td class="readme_td">Convert to cell of char arrays</td> </tr> </tbody> </table></markdown-accessiblity-table>

Properties

<markdown-accessiblity-table><table class="readme_table"> <thead> <tr> <th class="readme_th">Method</th> <th class="readme_th">Return type</th> <th class="readme_th">Description</th> </tr> </thead> <tbody> <tr> <td class="readme_td"><code>name</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">File or folder name without directory</td> </tr> <tr> <td class="readme_td"><code>parent</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Parent directory</td> </tr> <tr> <td class="readme_td"><code>root</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">First directory element of absolute paths</td> </tr> <tr> <td class="readme_td"><code>stem</code></td> <td class="readme_td"><code>string</code></td> <td class="readme_td">File name without extension</td> </tr> <tr> <td class="readme_td"><code>extension</code></td> <td class="readme_td"><code>string</code></td> <td class="readme_td">File extension</td> </tr> <tr> <td class="readme_td"><code>parts</code></td> <td class="readme_td"><code>string</code></td> <td class="readme_td">Split path into list comprising root, folders and name</td> </tr> <tr> <td class="readme_td"><code>strlength</code></td> <td class="readme_td"><code>double</code></td> <td class="readme_td">Number of characters in the path string</td> </tr> <tr> <td class="readme_td"><code>absolute</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Absolute path assuming the current working directory as reference</td> </tr> <tr> <td class="readme_td"><code>relative</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Path relative to reference directory</td> </tr> <tr> <td class="readme_td"><code>is</code></td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Whether properties match patterns</td> </tr> <tr> <td class="readme_td"><code>isAbsolute</code></td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Whether path is absolute</td> </tr> <tr> <td class="readme_td"><code>isRelative</code></td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Whether path is relative</td> </tr> </tbody> </table></markdown-accessiblity-table>

Modify

<markdown-accessiblity-table><table class="readme_table"> <thead> <tr> <th class="readme_th">Method</th> <th class="readme_th">Return type</th> <th class="readme_th">Description</th> </tr> </thead> <tbody> <tr> <td class="readme_td"> <code>/</code>, <code>\</code>, <code>join</code> </td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Join paths</td> </tr> <tr> <td class="readme_td"> <code>+</code>, <code>addSuffix</code> </td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Add string to the end of the path</td> </tr> <tr> <td class="readme_td"><code>setName</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Set file or folder name without directory</td> </tr> <tr> <td class="readme_td"><code>setParent</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Set parent directory</td> </tr> <tr> <td class="readme_td"><code>setRoot</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Set first directory element</td> </tr> <tr> <td class="readme_td"><code>setStem</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Set file name without extension</td> </tr> <tr> <td class="readme_td"><code>setExtension</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Set file extension</td> </tr> <tr> <td class="readme_td"><code>addStemSuffix</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Add string to the end of the file stem</td> </tr> <tr> <td class="readme_td"><code>regexprep</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/regexprep.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>regexprep</code></a> </td> </tr> <tr> <td class="readme_td"><code>tempFileName</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Append random unique file name</td> </tr> </tbody> </table></markdown-accessiblity-table>

Compare and filter

<markdown-accessiblity-table><table class="readme_table"> <thead> <tr> <th class="readme_th">Method</th> <th class="readme_th">Return type</th> <th class="readme_th">Description</th> </tr> </thead> <tbody> <tr> <td class="readme_td"> <code>==</code>, <code>eq</code> </td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Whether path strings are equal</td> </tr> <tr> <td class="readme_td"> <code>~=</code>, <code>ne</code> </td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Whether path strings are unequal</td> </tr> <tr> <td class="readme_td"><code>where</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Select paths where properties match patterns</td> </tr> </tbody> </table></markdown-accessiblity-table>

File system interaction

<markdown-accessiblity-table><table class="readme_table"> <thead> <tr> <th class="readme_th">Method</th> <th class="readme_th">Return type</th> <th class="readme_th">Description</th> </tr> </thead> <tbody> <tr> <td class="readme_td"><code>exists</code></td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Whether path exists in filesystem</td> </tr> <tr> <td class="readme_td"><code>isFile</code></td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Whether path is an existing file</td> </tr> <tr> <td class="readme_td"><code>isDir</code></td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Whether path is an existing directory</td> </tr> <tr> <td class="readme_td"><code>mustExist</code></td> <td class="readme_td">-</td> <td class="readme_td">Raise error if path does not exist</td> </tr> <tr> <td class="readme_td"><code>mustBeFile</code></td> <td class="readme_td">-</td> <td class="readme_td">Raise error if path is not an existing file</td> </tr> <tr> <td class="readme_td"><code>mustBeDir</code></td> <td class="readme_td">-</td> <td class="readme_td">Raise error if path is not an existing directory</td> </tr> <tr> <td class="readme_td"><code>modifiedDate</code></td> <td class="readme_td"><code>datetime</code></td> <td class="readme_td">Date and time of last modification</td> </tr> <tr> <td class="readme_td"><code>bytes</code></td> <td class="readme_td">-</td> <td class="readme_td">File size in bytes</td> </tr> <tr> <td class="readme_td"><code>mkdir</code></td> <td class="readme_td">-</td> <td class="readme_td">Create directory if it does not already exist</td> </tr> <tr> <td class="readme_td"><code>cd</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/cd.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>cd</code></a> </td> </tr> <tr> <td class="readme_td"><code>createEmptyFile</code></td> <td class="readme_td">-</td> <td class="readme_td">Create an empty file</td> </tr> <tr> <td class="readme_td"><code>delete</code></td> <td class="readme_td">-</td> <td class="readme_td">Delete files and directories. Remove directories recursively with optional argument <code>'s'</code>.</td> </tr> <tr> <td class="readme_td"><code>fopen</code></td> <td class="readme_td"><code>[double, char]</code></td> <td class="readme_td">Wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/fopen.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>fopen</code></a> </td> </tr> <tr> <td class="readme_td"><code>open</code></td> <td class="readme_td"><code>[double, onCleanup]</code></td> <td class="readme_td">Open file and return file ID and <code>onCleanup</code> object, which closes the file on destruction. Create parent directory if necessary. Raise error on failure.</td> </tr> <tr> <td class="readme_td"><code>readText</code></td> <td class="readme_td"><code>string</code></td> <td class="readme_td">Read text file</td> </tr> <tr> <td class="readme_td"><code>writeText</code></td> <td class="readme_td">-</td> <td class="readme_td">Write text file</td> </tr> <tr> <td class="readme_td"><code>copy</code></td> <td class="readme_td">-</td> <td class="readme_td">Copy to new path</td> </tr> <tr> <td class="readme_td"><code>copyToDir</code></td> <td class="readme_td">-</td> <td class="readme_td">Copy into target directory preserving the original name</td> </tr> <tr> <td class="readme_td"><code>move</code></td> <td class="readme_td">-</td> <td class="readme_td">Move to new path (rename)</td> </tr> <tr> <td class="readme_td"><code>moveToDir</code></td> <td class="readme_td">-</td> <td class="readme_td">Move into target directory preserving the original name</td> </tr> <tr> <td class="readme_td"><code>listFiles</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">List file paths in directory</td> </tr> <tr> <td class="readme_td"><code>listDeepFiles</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">List files paths in directory and all its subdirectories</td> </tr> <tr> <td class="readme_td"><code>listFolders</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">List directories in directory</td> </tr> <tr> <td class="readme_td"><code>listDeepFolders</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">List directories and subdirectories in directory</td> </tr> </tbody> </table></markdown-accessiblity-table>

Array

<markdown-accessiblity-table><table class="readme_table"> <thead> <tr> <th class="readme_th">Method</th> <th class="readme_th">Return type</th> <th class="readme_th">Description</th> </tr> </thead> <tbody> <tr> <td class="readme_td"><code>isEmpty</code></td> <td class="readme_td"><code>logical</code></td> <td class="readme_td">Check if array is empty</td> </tr> <tr> <td class="readme_td"><code>count</code></td> <td class="readme_td"><code>double</code></td> <td class="readme_td">Number of elements</td> </tr> <tr> <td class="readme_td"><code>sort</code></td> <td class="readme_td"><code>[Path, double]</code></td> <td class="readme_td">Sort by path string</td> </tr> <tr> <td class="readme_td"><code>unique_</code></td> <td class="readme_td"><code>[Path, double, double]</code></td> <td class="readme_td">Wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/unique.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>unique</code></a> </td> </tr> <tr> <td class="readme_td"><code>deal</code></td> <td class="readme_td"><code>[Path, Path, ...]</code></td> <td class="readme_td">Distribute array objects among output arguments</td> </tr> </tbody> </table></markdown-accessiblity-table>

Factories

<markdown-accessiblity-table><table class="readme_table"> <thead> <tr> <th class="readme_th">Method</th> <th class="readme_th">Return type</th> <th class="readme_th">Description</th> </tr> </thead> <tbody> <tr> <td class="readme_td"><code>Path.current</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Current working directory; wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/pwd.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>pwd</code></a> </td> </tr> <tr> <td class="readme_td"><code>Path.home</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">User home directory</td> </tr> <tr> <td class="readme_td"><code>Path.tempDir</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Temporary directory; wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/tempdir.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>tempdir</code></a> </td> </tr> <tr> <td class="readme_td"><code>Path.tempFile</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Random unique file in temporary directory; wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/tempname.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>tempname</code></a> </td> </tr> <tr> <td class="readme_td"><code>Path.matlab</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">MATLAB install directory; wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/matlabroot.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>matlabroot</code></a> </td> </tr> <tr> <td class="readme_td"><code>Path.searchPath</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Folders on MATLAB search path; wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/path.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>path</code></a> </td> </tr> <tr> <td class="readme_td"><code>Path.userPath</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">MATLAB user directory; wrapper for built-in <a href="https://www.mathworks.com/help/matlab/ref/userpath.html" rel="nofollow noopener noreferrer" target="_blank" rel="nofollow noopener noreferrer"><code>userpath</code></a> </td> </tr> <tr> <td class="readme_td"><code>Path.ofMatlabFile</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Path of MATLAB file on the MATLAB search path</td> </tr> <tr> <td class="readme_td"><code>Path.this</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Path of MATLAB file executing this method</td> </tr> <tr> <td class="readme_td"><code>Path.here</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Directory of MATLAB file executing this method</td> </tr> <tr> <td class="readme_td"><code>Path.empty</code></td> <td class="readme_td"><code>Path</code></td> <td class="readme_td">Empty object array</td> </tr> </tbody> </table></markdown-accessiblity-table>

Other

<markdown-accessiblity-table><table class="readme_table"> <thead> <tr> <th class="readme_th">Method</th> <th class="readme_th">Return type</th> <th class="readme_th">Description</th> </tr> </thead> <tbody> <tr> <td class="readme_td"><code>disp</code></td> <td class="readme_td">-</td> <td class="readme_td">Display in console</td> </tr> <tr> <td class="readme_td"><code>help</code></td> <td class="readme_td">-</td> <td class="readme_td">Open documentation web page</td> </tr> </tbody> </table></markdown-accessiblity-table>

Zitieren als

Martin Koch (2024). Path Class (https://github.com/MartinKoch123/Path/releases/tag/v2.2.1), GitHub. Abgerufen .

Kompatibilität der MATLAB-Version
Erstellt mit R2022a
Kompatibel mit R2019b und späteren Versionen
Plattform-Kompatibilität
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Veröffentlicht Versionshinweise
2.2.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v2.2.1

2.2.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v2.2.0

2.1.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v2.1.1

2.1.0.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v2.1.0

2.0.3

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v2.0.3

2.0.2

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v2.0.2

2.0.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v2.0.1

2.0.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v2.0.0

1.9.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.9.0

1.8.2

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.8.2

1.8.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.8.1

1.8.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.8.0

1.7.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.7.0

1.6.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.6.0

1.5.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.5.1

1.5.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.5.0

1.4.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.4.0

1.3.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.3.0

1.2.2

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.2.2

1.2.1

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.2.1

1.2.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.2.0

1.1.0

See release notes for this release on GitHub: https://github.com/MartinKoch123/Path/releases/tag/v1.1.0

1.0.0

Um Probleme in diesem GitHub Add-On anzuzeigen oder zu melden, besuchen Sie das GitHub Repository.
Um Probleme in diesem GitHub Add-On anzuzeigen oder zu melden, besuchen Sie das GitHub Repository.