Main Content

select

Class: matlab.buildtool.io.FileCollection
Namespace: matlab.buildtool.io

Filter file collection

Since R2023b

Description

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 matlab.buildtool.io.FileCollection 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.

example

Input Arguments

expand all

File collection, specified as a matlab.buildtool.io.FileCollection 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)

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Filter a file collection by using the select method.

Import the FileCollection class.

import matlab.buildtool.io.FileCollection

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
createFile(fullfile("source","file1.m"))
createFile(fullfile("source","file2.m"))
mkdir source private
createFile(fullfile("source","private","file3.m"))
createFile(fullfile("source","private","file4.m"))

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

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

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.

fc.paths'
ans = 4×1 string
    "source\file1.m"
    "source\file2.m"
    "source\private\file3.m"
    "source\private\file4.m"

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 = fc.select(@(p) ~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.

newfc.paths'
ans = 2×1 string
    "source\file1.m"
    "source\file2.m"

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.

createFile(fullfile("source","newFile1.m"))
newfc.paths'
ans = 3×1 string
    "source\file1.m"
    "source\file2.m"
    "source\newFile1.m"

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.

createFile(fullfile("source","private","newFile2.m"))
newfc.paths'
ans = 3×1 string
    "source\file1.m"
    "source\file2.m"
    "source\newFile1.m"

Local Function

This code shows the local function used in this example.

function createFile(filename)
fclose(fopen(filename,"w"));
end

Version History

Introduced in R2023b