Main Content

replace

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

Replace substrings in paths of file collection

Since R2023b

Description

example

newfc = replace(fc,old,new) replaces all occurrences of the substring old in the paths of the file collection fc with new. The method returns the new file collection as a matlab.buildtool.io.FileCollection array the same size as fc.

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

Substring to replace, specified as a string array, character vector, cell array of character vectors, or pattern array.

New substring, specified as a string array, character vector, or cell array of character vectors. If old contains multiple substrings, then new must either be the same size as old or be a single substring.

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Replace the file extension in the paths of a file collection by using the replace 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"

Create a file collection of corresponding P-code files by replacing .m with .p in the paths of fc. Then, return the paths of the new file collection.

newfc = fc.replace(".m",".p");
newfc.paths'
ans = 4×1 string
    "source\file1.p"
    "source\file2.p"
    "source\private\file3.p"
    "source\private\file4.p"

Add a file to the source folder, and return the paths of newfc again. Because the Glob pattern matches the newly created file as well, newfc has a path to the P-code file corresponding to newFile.m.

createFile(fullfile("source","newFile.m"))
newfc.paths'
ans = 5×1 string
    "source\file1.p"
    "source\file2.p"
    "source\newFile.p"
    "source\private\file3.p"
    "source\private\file4.p"

Local Function

This code shows the local function used in this example.

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

Tips

  • replace is a convenience method: newfc = fc.replace(old,new) is functionally equivalent to newfc = fc.transform(@(paths) replace(paths,old,new)).

Version History

Introduced in R2023b