主要内容

Match Files and Folders Using Patterns

R2026b
Since R2026b

MATLAB® uses a common pattern matching syntax across features that filter or select files and folders. For example, you can use patterns to automatically label project files based on their name or location, or to define which files or folders to ignore during code analysis.

The pattern matching syntax is based on wildcard characters, folder separators, and negation rules. Patterns are matched relative to a root folder, and you can combine multiple patterns to create complex inclusion and exclusion rules.

When you specify multiple patterns, MATLAB evaluates them in order and the last matching pattern determines whether a file is included or excluded.

This table shows the supported syntax for pattern matching:

FeatureSyntaxDescription
Wildcard*Matches zero or more characters in a single folder level. Does not match across folder separators.
Recursive wildcard**Matches zero or more folders. Use to match files at any depth in the folder hierarchy.
Folder separator/Separates folder levels in a pattern. On Windows®, backslash (\) is considered as a forward slash.
Negation prefix!Excludes files matched by previous patterns. Must appear at the start of the pattern.
Folder-only suffix/ (trailing)Matches folders only.

Wildcards

Use wildcard characters to match multiple files or folders with a single pattern. There are two types of wildcard:

  • Single wildcard — Matches in a single folder level.

  • Recursive wildcard — Matches across multiple levels of the folder hierarchy.

Single Wildcard (*)

The * character matches zero or more characters, but does not match across folder boundaries. Use it to match files in a single folder.

Example

To match all TXT files in the root folder, use:

*.txt

To match all CPP files inside the src folder, use:

src/*.cpp

Recursive Wildcard (**)

The ** pattern matches across folder boundaries and allows you to match files at any depth in the folder hierarchy. This character can appear at the beginning, middle, or end of a pattern.

Note

MATLAB treats three or more asterisks (***) as **.

Example

PositionPatternMatches
Leading**/fooA file or folder named "foo" at any depth, for example: foo, bar/foo, or bar/baz/foo.
Trailingabc/**Everything inside the "abc" folder recursively, for example: abc/file.txt or abc/sub/deep/file.m.
Middlea/**/bAny path from "a" to "b" with zero or more folders in between, for example a/b, a/x/b, a/x/y/b.

Folder Separator (/)

To separate folder levels in a pattern, use the forward slash, /. The behavior of the backslash (\) depends on the feature that supports the pattern matching syntax. For details, see the documentation of that feature.

  • On Windows — MATLAB functions treat the backslash (\) in patterns as a forward slash. For example, src/utils/*.m and src\utils\*.m are equivalent. However, patterns stored in a configuration file must be portable across all platforms and only accept forward slashes. For an example, see matlab.toml.

  • On Linux® — MATLAB treats the backslash as a literal character, not a separator.

Note

For compatibility across platforms, use the forward slashes in patterns.

Negation Prefix (!)

To exclude files matched by previous patterns, prefix a pattern with the ! character. The negation prefix allows you to create fine-grained rules by first including a broad set of files and then removing specific subsets.

  • Negation patterns only remove files from the matched set.

  • To re-include a file excluded by negation, use a later non-negated pattern.

  • MATLAB treats the ! character as a negation prefix only when it appears at the very start of a pattern. In any other position, MATLAB matches the ! character literally.

Example

Consider these patterns:

Patterns: ["*.cpp", "!test*.cpp", "test1.cpp"]

  • The first pattern includes all CPP files.

  • The second pattern excludes all files starting with test.

  • The third pattern re-includes the test1.cpp files specifically.

Folder-Only Suffix

To match folders only, use a pattern that ends with a forward slash (/). MATLAB then does not match a file of the same name.

Example

To match the src folder, but not a file named src, use this pattern:

src/ 

To match any folder named bin at any depth in the folder hierarchy, use this pattern:

**/bin/  

To match all directories at any depth in the folder hierarchy, use this pattern:

**/  

Escape Character

Some MATLAB features allow you to specify an escape character. An escape character causes MATLAB to treat the next character as a literal character instead of a special pattern character. Use an escape character when file names contain characters like * or !.

  • When you configure an escape character, placing it before a special character removes the special meaning of that character.

  • If you do not configure an escape character, all characters retain their special meaning.

  • You cannot use the characters /, *, and ! as the escape character.

Example

Configure the backslash \ as the escape character.

To match a file literally named file*.txt, use this pattern:

file\*.txt 

To match a file literally named !important.log, use this pattern:

\!important.log 

Examples

Match all TXT files in the root folder only.

*.txt

Match all CPP files inside the src folder.

src/*.cpp

Match all CPP files at any depth in the folder hierarchy.

**/*.cpp 

Match all CPP files under the src folder at any depth.

src/**/*.cpp

Match all CPP files in any folder named test.

**/test/*.cpp

Match all CPP files except those in test directories.

["**/*.cpp", "!**/test/**/*.cpp"]

Match all files except hidden files, but keep the .gitignore file.

["**/*", "!**/.*", "**/.gitignore"]

Match src/ but exclude build artifacts.

["src/**", "!**/*.o", "!**/*.obj", "!**/build/**"] 

With the escape character set to \, match data*.csv literally.

data\*.csv

With the escape character set to ^, match data*.csv literally.

data^*.csv

Tips

  • Use **/*.m to find all MATLAB files, regardless of folder depth.

  • Use negation to exclude generated or temporary files from a broad pattern. For example, to exclude Simulink cache files, use !**/*.slxc.

  • More specific patterns, such as src/utils/*.m, are more efficient than broad patterns with a leading ** character.

  • MATLAB evaluates patterns in order. Place more general patterns first, then narrow down with negation or more specific patterns.

See Also

| | | |

Topics