Main Content

Control Custom Table Parameter Programmatically

Manage custom table parameter in a block mask dialog programmatically by calling functions at the command-line. With these functions you can:

  • Create a custom table parameter.

  • Add and modify columns and rows within the table.

  • Modify the individual table cell properties.

  • Change the values of specific cells.

For information on creating a custom table from the Mask Editor user interface, see Customize Tables for Masked Blocks.

Add Custom Table Parameter to the Mask Dialog

Load the model and create a mask object.

open_system("slexMaskingBasic.slx")
maskObj = Simulink.Mask.get("slexMaskingBasic/mx + b");

Add custom table parameter vehicleDetails to the mask dialog.

tableParam = maskObj.addParameter(Name="vehicleDetails",Type="customtable");

Configure the columns of the Custom Table Parameter

You can insert, remove, and swap columns in a custom table parameter.

Add columns to the custom table parameter.

tableControl = maskObj.getDialogControl("vehicleDetails");
tableControl.addColumn(Name="Type of Vehicle",Type="edit");
tableControl.addColumn(Name="Vehicle class",Type="popup",TypeOptions={'Private','Commercial'});
tableControl.addColumn(Name="Out of State Plates",Type="checkbox");

Remove the first column from the table.

tableControl.removeColumn(2);

Add a column before the first column of the table.

tableControl.insertColumn(2,Name="Vehicle class",Type="popup",TypeOptions={'Private','Commercial'});

Display the properties of fourth column.

tableControl.getColumn(2)
ans = 
  TableParamColumnInfo with properties:

           Name: 'Vehicle class'
           Type: 'popup'
        Enabled: 'on'
        Visible: 'on'
          Width: 0
       Evaluate: 'off'
    TypeOptions: {2×1 cell}

Insert a new column between the first and second columns of the table with the Evaluate option selected. Setting the Evaluate option to on evaluates the expressions in the column.

tableControl.insertColumn(3,Name="No of Vehicles",Type="edit",Evaluate="on"); 

Specify the initial column width

tableControl.Columns(1).Width=150;

Set Value of the Custom Table Parameter

You can use the set_param and get_param functions to set or get the values of the custom table parameter like any other mask parameter.

set_param("slexMaskingBasic/mx + b","vehicleDetails","{'HDV','Commercial','20','on'}");
get_param("slexMaskingBasic/mx + b","vehicleDetails")
ans = 
'{'HDV','Commercial','20','on'}'

You can also use the mask parameter object to set or get the values of the custom table parameter like any other mask parameter. You can also use the setData function to set a value.

tableValue = join(["{'Cars','Private','800','off';", ...
    "'Buses','Private','50','off';}"]);
tableControl.setData(tableValue);

tableParam.Value = join(["{'Cars','Private','800','off';", ...
    "'Trucks','Private','20','on';","'Buses','Private','50','off';}"]);
tableParam.Value
ans = 
'{'Cars','Private','800','off'; 'Trucks','Private','20','on'; 'Buses','Private','50','off';}'

Create or Modify Rows of the Custom Table Parameter

Operations similar to those available for columns are also supported for rows. You can insert, remove, and swap rows in a custom table.

Add a row to the end of the table. Ensure that values passed is consistent with the column data.

tableControl.addRow("JCB","Private","2","on");

Insert a row after the second row in the table.

tableControl.insertRow(3,'Jeep','Private','50','off');

Remove the second row.

tableControl.removeRow(2);

Swap the third and fourth rows.

tableControl.swapRows(3,4);

Get the number of rows from the table.

tableControl.getNumberOfRows;

Get the index of the rows selected on the mask dialog. In this way you can query the selected rows in a table parameter callback.

tableControl.getSelectedRows;

Set and Get Cell Values

You can set and get the value of a particular cell in a custom table.

Get the value of the cell in the second row and first column.

tableControl.getValue([2 1]);

Set the value of the cell in the first row and third column to "20".

tableControl.setValue([1 3],"20");

Set and Get Cell Properties in the Mask Callback Code

To modify the properties of particular cells in a custom table, use the getTableCell, getChangedCells, and setTableCell functions. These functions must be used in mask callbacks and are useful for modifying cell properties based on the value of another cell.

To set and get cell properties of a custom table, use these steps:

1. Assign the mask callback file custom_table_cb to the mask.

maskObj.CallbackFile = "custom_table_cb";

This callback modifies cell properties based on the value of another cell. For example, it updates the Vehicle Class column options based on the value of Out of State Plates cell in the same row.

classdef custom_table_cb
    methods(Static)
        function vehicleDetails(callbackContext)
            blkHandle = callbackContext.BlockHandle; 
            maskObj = Simulink.Mask.get(blkHandle);
            tableControl = maskObj.getDialogControl("vehicleDetails");

            changedCells = tableControl.getChangedCells();
            if ~isempty(changedCells)
                rowIdx = changedCells{1}(1);
                colIdx = changedCells{1}(2);
                if(colIdx ~= 4)
                    return;
                end
                cellValue = tableControl.getValue([rowIdx, colIdx]);
                if(colIdx == 4 && strcmp(cellValue, 'on'))
                    tableControl.setTableCell([rowIdx 2],TypeOptions={'Interstate Bus', 'Gov goods'}, Value='Interstate Bus')
                else
                    tableControl.setTableCell([rowIdx 2],TypeOptions={'Private', 'Commercial'}, Value='Private')
                end
            end
        end
    end
end

2. Open the mask dialog to observe the changes applied by the callback code.

open_system("slexMaskingBasic/mx + b");

3. To modify the cell properties, use setTableCell in the mask callback code. For example, set the cell in the first row and fourth column as a Popup parameter.

tableControl.setTableCell([1 4],Type="popup",TypeOptions={'on','off'},Value='off');

Note: You must use setTableCell and getTableCell functions within a mask callback code.

4. To get the cell properties, use getTableCell in the mask callback code. For example, get the properties of the cell in the first row and third column.

tableCell = tableControl.getTableCell([1 3]);

See Also

Classes

Functions

Topics