Main Content

Extend MATLAB Using Extension Points (Beta)

Since R2024b

You can extend various functionalities of the MATLAB® desktop using extension points. For example, you can use extension points to add items to the Files panel context menu or to the quick access toolbar.

You can use these extension points to extend the MATLAB desktop:

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

Get Started with Using Extension Points

To use extension points, create a JSON-formatted file named extensions.json and add a set of JSON declarations to the file. Then, to enable your customizations, add extensions.json to the path.

For example, this extensions.json file uses the mw.desktop.quickAccess extension point to add an item to the quick access toolbar and the mw.desktop.fileBrowser.contextMenu extension point to add an item to the Files panel context menu.

{
    "mw.desktop.quickAccess": {
        "items": [
        {
            "name": "openCustomFunction",
            "type": "PushButton",
            "text": "Open Custom Function",
            "description": "Open my custom function",
            "icon": "./open_16.png",
            "command": "openMyFunction"
        }]
    },
    "mw.desktop.fileBrowser.contextMenu": {
        "customSection": {
            "items": [
            {
                "name": "displayAttributes",
                "type": "MenuItem",
                "text": "Display File Attributes",
                "description": "Tooltip hover information",
                "icon": "./icons/Display_16.png",
                "command": "displayFileAttributes"
            }]
         }
     }
}

Create extensions.json

To start extending MATLAB, create a JSON-formatted file named extensions.json and place it in a folder named resources. You can create more than one extensions.json file, as long as they are in different resources folders.

Add JSON Declarations

For each extension point that you want to use, add a set of JSON declarations to the extensions.json file.

The extensions.json file should contain a single top-level JSON object. MATLAB extension points are organized by category in a tree-based hierarchy under the top-level namespace, mw.

JSON uses braces to define objects and refers to objects as collections of name and value pairs. Because these terms are overloaded in context of MATLAB, this documentation uses the term "property" instead of "name." JSON uses brackets to define arrays.

Create-User Defined MATLAB Function

Some extension point properties call user-defined MATLAB functions to perform an action. When creating these functions, specify the required input arguments and no output arguments. Place your function in a folder on the MATLAB path to make it available.

Enable Your Customizations

After you are done creating the extensions.json file, to enable your customizations, add the folder containing the resources folder and extensions.json 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.

Add Items to Files Panel Context Menu

You can add your own custom items to the Files panel context menu using the mw.desktop.fileBrowser.contextMenu extension point.

For each custom item that you want add to the Files panel context menu, define an object within the items array of the mw.desktop.fileBrowser.contextMenu extension point. When adding multiple custom items, separate each object with a comma.

For example, this extensions.json file contains a set of JSON declarations that adds an item to the Files panel context menu.

{
    "mw.desktop.fileBrowser.contextMenu": {
        "customSection": {
            "items": [ 
            {
                "name": "displayAttributes",
                "type": "MenuItem",
                "text": "Display File Attributes",
                "icon": "./icons/Display_16.png",
                "command": "displayFileAttributes"
             }]
        }
    }
}

Create-User Defined MATLAB Function

To perform an action when a custom item in the Files panel context menu is selected, create a function that performs the intended action. Then specify the function name as the value of the command property. Place the function in a folder on the MATLAB path to make it available.

For example, create the function displayFileAttributes, which is referenced by the command property in the JSON file. MATLAB calls this function when you select the custom displayAttributes item in the Files panel context menu. When called, the function displays the file attributes for each selected file in the Files panel.

function displayFileAttributes(selectedFilePaths)
numfiles = length(selectedFilePaths);
for i = 1:numfiles
    disp("File attributes for '" + selectedFilePaths(i) + "'")
    fileattrib(selectedFilePaths(i))
end
end

Enable Your Customizations

To enable your customizations, add the folder containing the resources folder and the extensions.json file to the path. When the folder is added to the path, a new menu item appears in the Files panel context menu.

Files panel context menu with a Display File Attributes item at the bottom of the menu

When you select the new menu item, MATLAB displays the file attributes for the selected files in the Files panel within the Command Window.

File attributes for 'C:\MyWork\stat.m'

            Name: 'C:\MyWork\stat.m'
         archive: 1
          system: 0
          hidden: 0
       directory: 0
        UserRead: 1
       UserWrite: 1
     UserExecute: 1
       GroupRead: NaN
      GroupWrite: NaN
    GroupExecute: NaN
       OtherRead: NaN
      OtherWrite: NaN
    OtherExecute: NaN

Add Items to Quick Access Toolbar

You can add your own custom items to the quick access toolbar using the mw.desktop.quickAccess extension point.

For each custom item that you want add to the quick access toolbar, define an object within the items array of the mw.desktop.quickAccess extension point. When adding multiple custom items, separate each object with a comma.

For example, this extensions.json file uses the mw.desktop.quickAccess extension point to add a push button to the quick access toolbar.

{
    "mw.desktop.quickAccess": {
        "items": [
            {
                "name": "openCustomFunction",
                "type": "PushButton",
                "text": "Open Custom Function",
                "description": "Open my custom function",
                "icon": "./icons/Open_16.png",
                "command": "openMyFunction"
            }]
    }
}

Create User-Defined MATLAB Function

To perform an action when a custom item in the quick access toolbar is clicked or selected, create a function that performs the intended action. Then specify the function name as the value of the command property. Place the function in a folder on the MATLAB path to make it available.

For example, create the function openMyFunction, which is referenced by the command property in the JSON file. MATLAB calls this function when you click the custom openCustomFunction button in the quick access toolbar. When called, the function prints a message in the Command Window and opens myWorkingFile.m in the Editor.

function openMyFunction
disp("Opening 'myWorkingFile.m' in the Editor...")
edit myWorkingFile.m
end

Enable Your Customizations

To enable your customizations, add the folder containing the resources folder and the extensions.json file to the path. When the folder is added to the path, a new button appears in the quick access toolbar.

Toolstrip with a new push button in the quick access toolbar

When you click the button, the openMyFunction function is called .openMyFunction opens the file myWorkingFile.m in the Editor and prints this text to the Command Window:

Opening 'myWorkingFile.m' in the Editor...

Customize How Files Display in MATLAB

You can customize how to display file types in MATLAB using extension points.

Specify Icon and Label

You can customize the icon and label for file types in MATLAB. For each file type that you want to customize, define an object within the mw.fileTypes.icons and mw.fileTypes.labels extension points. To specify an icon, specify the object name as the extension of the file type and the object value as the path to the icon. To specify a label, specify the object name as the extension of the file type, and the object value as the label text. When adding icons and labels for multiple file types, separate each object with a comma.

For example, this set of JSON declarations adds a custom icon and label for markdown files (.md) and SVG files (.svg).

{
    "mw.fileTypes.icons": {
        "md": "./documentList.svg",
        "svg": "./images.svg"
    },
    "mw.fileTypes.labels": {
        "md": "Markdown Documentation File",
        "svg": "Support Vector Graphics"
    }
}

For more information, see mw.fileTypes.icons and mw.fileTypes.labels.

Specify Filename Validation

You can add filename validation for file types. For each file type, define an object within the mw.fileTypes.filenameValidation extension point. When adding filename validation for multiple file types, separate each object with a comma.

Within each file type object, define 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. Then, for each errorRegexPatterns and warningRegexPatterns object, specify a pattern to valid against, any validation flags, and the error or warning message to display.

For example, this set of JSON declarations adds filename validation for markdown files (.md) to check whether the name of a markdown file is not empty or too long when the file is created or renamed.

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

For more information, see mw.fileTypes.filenameValidation.

Create Groups of File Types

You can create groups of file types in MATLAB using the mw.fileTypes.groups extension point. Creating groups allows you to customize multiple file types at the same time.

To create a group of file types, define an object within the mw.fileTypes.groups extension point. Specify the object name as the name of the group that you want to create and the object value as an array of file type extensions that you want to include in your group. When creating multiple groups, separate each object with a comma. Then, to use the group to customize multiple file types at the same time, specify groups.groupname instead of a file type extension.

For example, this set of JSON declarations creates a group for markdown files (.md) and SVG files (.svg), and then customizes the icon and label for the group.

{
    "mw.fileTypes.groups": {
        "myfiletype": ["md", "svg"]
    },
    "mw.fileTypes.icons": {
        "groups.myfiletype": "./images.svg"
    },
    "mw.fileTypes.labels": {
        "groups.myfiletype": "My file types"
    }
}

For more information, see mw.fileTypes.groups.

Enable Your File Type Customizations

To enable your file type customizations, add the folder containing the resources folder and the extensions.json file to the path. When the folder is added to the path, files with a .md or .svg extension appear in the Files panel with the icon and label specified in extensions.json. If the Type column is not visible in the Files panel, go to the top-right corner of the panel, click the Files actions button , and select Show > Type.

Files panel with updated icons and labels for the images.svg and README.md files

See Also

| | | | | |

Related Topics