Main Content

mw.fileTypes.filenameValidation Extension Point (Beta)

Add filename validations for file type

Since R2024b

Note

Extension points are available only as part of the new desktop for MATLAB®. The new desktop for MATLAB is in beta development and should not be used for development or production activities. For more information, see Get Started with the New Desktop for MATLAB (Beta).

You can add filename validations for different file types in MATLAB using the mw.fileTypes.filenameValidation extension point. Adding filename validation for a file type ensures that when a file of that type is created or renamed, the filename is valid.

To add filename validations for file types in MATLAB:

  1. Create a JSON-formatted file named extensions.json and place it in a folder named resources.

  2. Add a set of JSON declarations to extensions.json that defines one or more filename validations.

  3. Add the folder containing the resources folder to the MATLAB path. To add the folder to the path, use the addpath function or right-click the folder in the Files panel and select Add to Path > Selected Folders and Subfolders.

This JSON code shows the basic structure of the mw.fileTypes.filenameValidation extension point.

{
    "mw.fileTypes.filenameValidation": {
        "groups.matlabCodeFiles": {
            "errorRegexPatterns": [{
                "pattern": "/^.+$/;",
                "flags": "ig",
                "errorLabel": "Filename must not be empty"
            }],
            "warningRegexPatterns": [{
                "pattern": "/^.{0,125}$",
                "flags": "ig",
                "warningLabel": "Filename is too long"         
            }]
        }
    }
}

For more information about using extension points, see Extend MATLAB Using Extension Points (Beta).

Properties

expand all

Required Properties

Filename validation, specified as one or more errorRegexPatterns or warningRegexPatterns objects. When a file of the specified file type is created or renamed, MATLAB uses the file type filename validation to ensure that the filename is valid.

Specify the property name as the lowercase file type extension or file type group that you want to add the file type filename validation for. Specify the property value as one or more errorRegexPatterns or warningRegexPatterns objects, separating each object with a comma. Include an errorRegexPatterns or warningRegexPatterns object for each pattern that you want to validate for. For more information about file type groups, see mw.fileTypes.groups.

Error pattern for a specified file type, specified as an array of these properties:

  • pattern

  • flags

  • warningLabel

Warning pattern for a specified file type, specified as an array of these properties:

  • pattern

  • flags

  • errorLabel

Regular expression pattern, specified as a JSON string.

Specify pattern as a JavaScript® regular expression that defines a valid filename pattern for the specified file type. If the regular expression fails, then MATLAB displays the associated errorLabel or warningLabel text.

For example, this regular expression states that the filename must not be empty:

Example: "pattern": "/^.+$/;"

This regular expression states that the filename must contain less than 125 characters:

Example: "pattern": "/^.{0,125}$"

For more information about JavaScript regular expressions, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions.

Filename validation flag, specified as a JSON string.

Valid filename validation flags include:

  • "i" — Search is case insensitive.

  • "g" — Search for all matches. If "g" is not specified, only the first match is returned.

  • "m" — Allow ^ and $ to match next to newline characters.

  • "s" — Allow dot (.) to match newline characters.

  • "u" — Enable full Unicode support.

  • "y" — Enable searching at the current position.

For more information about flags, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions.

Example: "flags": "ig"

Error label to display when the filename does not match the specified pattern, specified as a JSON string.

Example: "errorLabel": "Filename must not be empty"

Warning label to display when the filename does not match the specified pattern, specified as a JSON string.

Example: "warningLabel": "Filename is too long"

Version History

Introduced in R2024b