Contenuto principale

slValidateContextMenu

R2026b

Validate and debug JSON files that define context menu customization

Since R2026a

    Description

    [isValid,errors] = slValidateContextMenu(filePath) validates the extensions.json defining a context-menu customization. filePath specifies the parent folder of the resources folder containing the JSON file. The function returns a value indicating whether the validation passed and error messages for any errors encountered during validation. The parent folder must be on the MATLAB® path.

    If the validation fails, debug the file by reading the error messages and checking the file against the information in RichMenu Extension.

    example

    Examples

    collapse all

    Suppose you want to add a menu item to the Simulink® context menu, and this is the extensions.json file that defines your customization.

    {
        "mw.schemaVersion": "1.0.0",
    
        "mw.simulink.editor.contextMenu.focusMenu:after":
            ["#myCustomMenus.myMenuSection"]
    
        "myCustomMenus.myMenuSection":
        {
            "type": "MenuSection",
            "items": ["#myCustomMenus.myMenuItem"]
        },
    
        "myCustomMenus.myMenuItem":
        {
            "type": "MenuItem",
            "action": "#myCustomMenus.checkForAlgLoops"
        },
    
        "myCustomMenus.checkForAlgLoops":
        {
            "type": "Action",
            "text": "Check for Algebraic Loops",
            "description": "Check current system for algebraic loops",
            "callback": "check"
        }
    }

    The extensions.json file is located at C:\myDocuments\myCustomization\resources\extensions.json.

    Validate and debug the file.

    1. Add the parent folder of the resources folder to the MATLAB path.

      myFilePath = C:\myDocuments\myCustomization;
      addpath(myFilePath);
    2. Run the slValidateContextMenu function.

      [isValid,errorMsg] = slValidateContextMenu(myFilePath)
      isValid =
        logical
         0
      errorMsg = 
        struct with fields:
      
             message: '[error]: Reading contents of file failed. Exception: 'Parse Error. No trailing '}' while parsing value of type object (possible missing comma?)'.↵Resource Name: mw.simulink.editor.contextMenu↵File Path: C:\Users\myName\addMenuItems\resources\extensions.json↵(Ln: 7, Col: 5)'
          identifier: 'registration_framework:reg_fw_resources:invalidDefinitionFor'
             details: [1×1 MException]

      The value of isValid is 0, indicating that the validation failed.

    3. Use the error message the slValidateContextMenu output to find the mistake. The message hints that a comma might be missing.

      Looking closely at the extensions.json file, you can see that there is a comma missing after ["#myCustomMenus.myMenuSection"] (fifth line from the top). Add the comma.

    4. Run the slValidateContextMenu function again.

      [isValid, errorMsg] = slValidateContextMenu(myFilePath)
      isValid =
        logical
         1
      errorMsg =
           []

      The value of isValid is 1, indicating that the validation passed.

    Input Arguments

    collapse all

    Absolute file path to the parent folder of the resources folder containing the extensions.json file you want to validate, specified as a string or character vector.

    Example: "C:/Users/myName/Documents/myCustomizations"

    Output Arguments

    collapse all

    Status indicating whether the specified extensions.json file contains errors, returned as one of:

    • 1 — Contains no errors

    • 0 — Contains errors

    Structure array with the fields message, identifier, and details. If the function finds errors in the specified extensions.json file, the software stores the error messages in the message field.

    Version History

    Introduced in R2026a