Main Content



Filter file collection

Since R2023b



newfc = select(fc,fcn) selects the files and folders in the file collection fc whose paths satisfy the conditions of the function handle fcn. The method returns the filtered file collection as a column vector.

You can use newfc to specify task inputs and outputs. For more information about task inputs and outputs, see Improve Performance with Incremental Builds.

Input Arguments

expand all

File collection, specified as a array.

Path selection function, specified as a function handle. The function must accept the paths of fc as an input and return a logical array the same size as that input. The logical array must use 1 (true) to indicate each path the function selects and 0 (false) for each unselected path.

Example: @(paths) ~contains(paths,"buildfile.m")

Example: @(paths) endsWith(paths,".m")

Example: @(paths) endsWith(paths,".m") & ~contains(paths,"buildfile.m")

Example: @(paths) isfile(paths)



To learn about attributes of methods, see Method Attributes.


expand all

Filter a file collection by using the select method.

Import the FileCollection class.


Create the folder structure used in this example. See the code of the local function createFile, which is used to create the files, at the end of this example.

mkdir source
mkdir source private

Create a file collection from all the .m files in the source folder and any of its subfolders. In this example, fc is a object because it is defined by a pattern that includes the * and ** wildcards.

fc = FileCollection.fromPaths("source/**/*.m")
fc = Glob

Return the paths of the file collection. When you call paths on a Glob object, the method returns the paths to the files and folders on disk that match the Glob pattern.

ans = 4×1 string

Filter fc by excluding the files in the private subfolder. This code selects the files in fc whose paths do not contain the substring "private".

newfc = ~contains(p,"private"));

Return the paths of the filtered file collection. The paths method returns the paths to the files and folders on disk that match the Glob pattern and that satisfy the condition of the specified function handle.

ans = 2×1 string

Add a file to the source folder, and return the paths of the filtered file collection again. newfc has an additional path because the newly created file matches the Glob pattern and its path satisfies the condition of the function handle.

ans = 3×1 string

If you add a file to the private folder, the file does not affect the filtered file collection because the file path does not satisfy the condition of the function handle.

ans = 3×1 string

Local Function

This code shows the local function used in this example.

function createFile(filename)

Version History

Introduced in R2023b