Extend MATLAB Using Extension Points
You can extend various functionalities of MATLAB® using extension points. For example, you can use extension points to add items to the quick access toolbar or to the Files panel context menu.
To use extension points:
Create a JSON-formatted file named
extensions.json
.Add a set of JSON declarations to the file.
Enable your customizations by adding
extensions.json
to the path.
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. Add the JSON declarations to a single top-level
JSON object in the extensions.json
file. 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 the context of MATLAB, this documentation uses the term "property" instead of "name." JSON uses brackets to define arrays.
For example, this set of JSON declarations uses the
mw.desktop.quickAccess
extension point to add an item to the quick
access toolbar and the mw.desktop.fileBrowsers.contextMenu
extension
point to add an item to the Files panel context menu.
{ "mw.desktop.quickAccess": { "items": [ { "name": "openCustomFunction", "type": "PushButton", "action": { "text": "Open Custom Function", "description": "Open my custom function", "icon": "./icons/Open_16.png", "callback": "openMyFunction" } } ] }, "mw.desktop.fileBrowsers.contextMenu": { "sections": [ { "name": "myToolbox.myCustomSection", "type": "SimpleMenuSection", "items": [ { "name": "myToolbox.myItem1", "type": "SimpleMenuItem", "when": "selection.isEmpty", "action": { "name": "myToolbox.myItem1Action", "type": "Action", "text": "Item 1", "icon": "./icons/Display_16.png", "callback": "displayFileAttributes" } } ] } ] } }
Enable Your Customizations
After creating the extensions.json
file, to enable your
customizations, add the folder containing the resources
folder with the
extensions.json
file 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.
Use References with Extension Points
To reduce nesting within the extensions.json
file, you can move the
definition of objects out of your main extension point definition and then reference those
objects within the definition. Moving object definitions out of the extension point
definition effectively flattens the code structure and avoids deep nesting levels, improving
the readability and maintainability of the extensions.json
file.
To define an object outside of the main extension point structure, add the JSON
declaration for the object after the main extension point definition, using a unique
identifier to identify the object. If the object includes a name
property, omit this property from the object definition. Then, use the format
#
to reference the object,
where referenceObject
referenceObject
is the unique identifier for the object.
For example, this code shows how to use reference objects with the
mw.desktop.fileBrowsers.contextMenu
extension point to add an item to
the Files panel context menu.
{ "mw.desktop.fileBrowsers.contextMenu": { "sections": [ "#myToolbox.myCustomSection" ] }, "myToolbox.myCustomSection": { "type": "SimpleMenuSection", "items": [ "#myToolbox.myItem1" ] }, "myToolbox.myItem1": { "type": "SimpleMenuItem", "when": "selection.isEmpty", "action": "#myToolbox.myItem1Action" }, "myToolbox.myItem1Action": { "type": "Action", "text": "Item 1", "icon": "./icons/Display_16.png", "callback": "displayFileAttributes" } }
See Also
mw.desktop.fileBrowsers.contextMenu Extension Point
| mw.desktop.quickAccess
Extension Point