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