Main Content

matlab.alias.AliasFileManager class

Package: matlab.alias

Create and manage class alias definitions

Description

The matlab.alias.AliasFileManager class enables you to create and manage class alias definitions. The alias definitions map a new class name to one or more old names. Use this functionality to change the name of an existing class while maintaining compatibility with code and MAT-files that use the older names of the class.

The recommended process for creating aliases is to write a function to create an instance of AliasFileManager. For more information and examples, see Creating and Managing Class Aliases.

The matlab.alias.AliasFileManager class is a handle class.

Class Attributes

Sealed
true

For information on class attributes, see Class Attributes.

Creation

Description

example

fileMgr = matlab.alias.AliasFileManager creates a default AliasFileManager instance.

fileMgr = matlab.alias.AliasFileManager(location) creates an AliasFileManager instance and loads the alias file in the specified location. The location is the path to the parent folder of the resources folder containing an alias definition file. Using this syntax provides a convenient way to view all of the defined class aliases at location.

Input Arguments

expand all

The path to a folder containing an existing class alias file, specified as a string scalar. The path must point to the parent folder of the resources folder containing an alias definition file. If no alias file exists at that location, MATLAB® errors.

location can be a full or partial path. Full paths are not recommended for code that is packaged for use by others because installation paths might vary.

Properties

expand all

Class alias definitions, specified as an array of AliasDefinition objects. The property contains current class name and all active old aliases for all classes in a given folder.

Attributes:

Transient
true

Methods

expand all

Examples

collapse all

This example shows how to use a function to create a class alias file.

This example renames the class FirstName to SecondName. To accomplish this, an alias definition file is created that identifies SecondName as the new class name and FirstName as an old alias.

The class file with the new name, SecondName.m, is in a folder named Work. The function createAliasFile is also in Work.

Work folder with createAliasFIle and SecondName files

The createAliasFile function is run from the Work folder. The function creates fileMgr, an instance of class AliasFileManager. The addAlias method adds the new and old names of the class to fileMgr. Finally, the writeAlias method writes the alias definition file inside a resources folder. The method creates the folder if it does not already exist.

function createAliasFile
    fileMgr = matlab.alias.AliasFileManager;
    addAlias(fileMgr,NewName="SecondName",OldNames="FirstName");
    writeAliasFile(fileMgr);
end

Change the current working directory to the Work folder and call createAliasFile.

createAliasFile

The function creates the resources folder and the alias definition file inside of it.

resources folder added, containing alias definition file

MATLAB now recognizes both class names, and instances constructed using either name are defined as instances of SecondName.

old = FirstName
new = SecondName
old = 

  SecondName with no properties.


new = 

  SecondName with no properties.

Tips

  • The function workflow demonstrated in the example has the advantage of automatically clearing the instance of AliasFileManager.

  • Deprecating old class aliases is not recommended. Removing the old aliases risks compatibility issues for the users of your code. To access an existing alias definition file, use the fileMgr = matlab.alias.AliasFileManager(location) constructor and read its Aliases property.

Introduced in R2021b