Main Content

Improve Code Efficiency by Merging Multiple Interpolation Using Prelookup Blocks

You can use the Model Transformer tool to refactor a modeling pattern to improve the efficiency of generated code. The Model Transformer identifies and merges multiple Interpolation Using Prelookup blocks that have same input signals connected from the outputs of Prelookup blocks into a single Interpolation Using Prelookup block.

The Model Transformer works if the properties of Interpolation Using Prelookup blocks are same except for Table data. Reducing the number of Interpolation Using Prelookup blocks in a model reduces the number of variable assignments in the code, which improves the efficiency of the generated code. You can use the Model Transformer app or programmatic commands to refactor the model.

The Model Transformer can replace multiple Interpolation Using Prelookup that:

  • Have the same input signals connected to Prelookup blocks with the same index and fraction parameters

  • Have the output signals connected to the same Multiport Switch block

  • Have the same breakpoint specification, values, and data types

  • Have the same algorithm parameters

  • Have the same data type for fraction parameters

Merge Interpolation Using Prelookup Blocks Using the Model Transformer App

This example shows how to use the Model Transformer app and APIs to identify redundant Interpolation Using Prelookup blocks, and then refactor the model.

The model mInterpolationOptim uses Prelookup blocks to input signals to several Interpolation Using Prelookup blocks. The output of these Interpolation blocks are connected to a Multiport Switch block.

In this example, you identify Interpolation Using Prelookup blocks that qualify for transformation and replace them with a single Interpolation Using Prelookup block and Constant blocks connected to Multiport Switch block.

  1. Open the model mInterpolationOptim. At the MATLAB® command line, enter:

    addpath(fullfile(docroot,'toolbox','simulink','examples'))
    mInterpolationOptim

  2. Save the model to your working folder.

  3. On the Apps tab, click Model Transformer.

  4. In the Transformations folder, select the Replace Interpolation Using Prelookup Blocks check.

  5. Select the Skip Interpolation-ND blocks in libraries from this transformation option to avoid replacing Interpolation Using Prelookup blocks that are linked to a library.

  6. In the Prefix of transformed model field, specify a prefix for the refactored model.

  7. Click Run This Check. The top Result table contains hyperlinks to the Interpolation Using Prelookup blocks and the corresponding Multiport Switch block port indices.

  8. Clear the check boxes under Candidate Groups for the groups that you do not want to transform.

  9. Click Refactor Model. The Result table contains a hyperlink to the new model. The table contains hyperlinks to the shared Interpolation Using Prelookup blocks and corresponding Multiport Switch block ports. The tool also creates an m2m_mInterpolationOptim folder that contains the new gen_mInterpolationOptim.slx model.

The two Prelookup blocks Prelookup and Prelookup1 in the gen_mInterpolationOptim.slx model connect to the single Interpolation Using Prelookup block and the Constant blocks are connected to the Multiport Switch block port to give the Table data as input.

Merge Interpolation Using Prelookup Blocks Programmatically

To use the Model Transformer programmatically, use:

  1. Save the model in the current working directory.

    addpath(fullfile(docroot,'toolbox','simulink','examples'))
    mInterpolationOptim
  2. To identify candidates qualified for transformation, use the function Simulink.ModelTransform.CommonSourceInterpolation.identifyCandidates to create the object transformResults.

    transformResults = Simulink.ModelTransform.CommonSourceInterpolation.identifyCandidates('mInterpolationOptim')
    
    transformResults =
    
      Results with properties:
    
        Candidates: [1×1 struct]

    The transformResults object has one property, Candidates, that is a structure with two fields, InterpolationPorts and SwitchPorts.

    
    transformResults.Candidates = 
    
      struct with fields:
    
            InterpolationPorts: [4×1 struct]
                   SwitchPorts: [4×1 struct]

  3. View the InterpolationPorts field.

    transformResults.Candidates.InterpolationPorts = 
      4×1 struct array with fields:
    
        Block
        Port

    The InterpolationPorts field consists of two arrays, Block and Port. Similarly, the SwitchPorts has the same properties.

  4. Convert the Candidates.InterpolationPorts and Candidates.SwitchPorts fields to tables.

    struct2table(transformResults.Candidates.InterpolationPorts)
    struct2table(transformResults.Candidates.SwitchPorts)
    ans =
      4×2 table
                            Block                                Port
        _____________________________________________            ____
        {'mInterpolationOptim/Interpolation↵Using Prelookup' }     0  
        {'mInterpolationOptim/Interpolation↵Using Prelookup1'}     0  
        {'mInterpolationOptim/Interpolation↵Using Prelookup2'}     0  
        {'mInterpolationOptim/Interpolation↵Using Prelookup3'}     0  
    
    
    ans =
      4×2 table
                    Block                           Port
        ______________________________              ____
        {'mInterpolationOptim/Multiport_Switch'}     1  
        {'mInterpolationOptim/Multiport_Switch'}     2  
        {'mInterpolationOptim/Multiport_Switch'}     3  
        {'mInterpolationOptim/Multiport_Switch'}     4  

    Use the SwitchPorts to see which Interpolation Using Prelookup blocks are connected to which Multiport Switch block port.

  5. To refactor the model, use the function Simulink.ModelTransform.CommonSourceInterpolation.refactorModel. This function uses the object transformResults from identifyCandidate function.

    refactorResults = Simulink.ModelTransform.CommonSourceInterpolation.refactorModel(transformResults)
    
    refactorResults = 
    
      RefactorResults with properties:
    
               ModelName: 'mInterpolationOptim'
          ModelDirectory: ''
        TraceabilityInfo: [4×1 containers.Map]

    The ModelName and ModelDirectory properties of the refactorResults object list the name and location of the refactored model. TraceabilityInfo is a containers.Map object that lists the block tracing information.

Conditions and Limitations

The Model Transformer cannot replace Interpolation Using Prelookup blocks if:

  • The Interpolation Using Prelookup blocks are in commented-out regions or inactive variants.

  • The Interpolation Using Prelookup blocks are masked.

  • The Model Transformer app does not replace Interpolation Using Prelookup blocks across the boundaries of atomic subsystems, referenced models, or library-linked blocks.

Related Topics