Main Content

Find Clones Across the Model

Clones are modeling patterns that have identical block types and connections. You can refactor your model by creating library blocks from these clone patterns and replacing the clones with links to the library blocks, which enable you to reuse the components. For more information about clones, see Enable Component Reuse by Using Clone Detection.

You can search for clones in a subsystem or anywhere across the model.

  • Subsystem clones: Identifies clones only in a subsystems.

  • Clones across model: Identifies clones anywhere across the model.

This example shows how to use the Clone Detector app and APIs to identify clones anywhere across the model, and then replace them with links to library blocks.

Identify Clones Across the Model by Using the Clone Detector App

This example shows how to identify clones across the model by using the Clone Detector.

  • Open the model similar_clones_model.

  • Save the model in the current working directory.

  • On the Apps tab, click Clone Detector. Alternatively, at the MATLAB command line, enter: clonedetection("similar_clones_model").

  • To set up the parameters for clone detection, click Settings. Under Clone anywhere settings, click Detect Clones Across Model.

Minimum Region Size and Minimum Clone Group Size are set to 2 by default. The Minimum Region Size parameter represents the minimum blocks needed per clone region and the Minimum Clone Group Size parameter represents the minimum clone occurrences needed to define it as a clone group.

  • Click Find Clones to identify clones.

  • The app highlights the clones. Exact clones are highlighted in red and similar clones are highlighted in blue.

The clones highlighted in this example include clones identified in and outside of subsystems and a Simulink block connected to a subsystem.

  • Click Replace Clones.

The app refactors the model and replaces the clones with links to the newLibraryFile library file in your working directory. The app replaces the Simulink clone blocks outside of the subsystems with linked Subsystem blocks.

Identify Clones Programmatically

  1. Use the Simulink.CloneDetection.Settings class to create an object.

     cloneDetectionSettings = Simulink.CloneDetection.Settings()
     cloneDetectionSettings = 
                                IgnoreSignalName: 0
                             IgnoreBlockProperty: 0
                          ExcludeModelReferences: 0
                             ExcludeLibraryLinks: 0                      
                  FindClonesRecursivelyInFolders: 1
                        ParamDifferenceThreshold: 50
        ReplaceExactClonesWithSubsystemReference: 0
                                       Libraries: {}
                                         Folders: {}
                         DetectClonesAcrossModel: 0
                          ExcludeInactiveRegions: 0
  2. To search for clones across the model, set DetectClonesAcrossModel to 1.

    cloneDetectionSettings.DetectClonesAcrossModel = 1
    cloneDetectionSettings = 
    
                                IgnoreSignalName: 0
                             IgnoreBlockProperty: 0
                          ExcludeModelReferences: 0
                             ExcludeLibraryLinks: 0
                          SelectedSystemBoundary: []
                  FindClonesRecursivelyInFolders: 1
                        ParamDifferenceThreshold: 50
        ReplaceExactClonesWithSubsystemReference: 0
                                       Libraries: {}
                                         Folders: {}
                         DetectClonesAcrossModel: 1
                          ExcludeInactiveRegions: 0
                               MinimumRegionSize: 2
                           MinimumCloneGroupSize: 2

    MinimumRegionSize and MinimumCloneGroupSize are set to 2 by default. You can change their values.

  3. To find clones, execute the function Simulink.CloneDetection.findClones using the cloneDetectionSettings object.

    cloneResults = Simulink.CloneDetection.findClones('similar_clones_model',cloneDetectionSettings)
    cloneResults = 
    
              Clones: [1×1 struct]
        ExceptionLog: ''
    cloneResults.Clones = 
      Results with properties:
    
            Summary: [1×1 struct]
        CloneGroups: [1×2 struct]

    For more details on the clone detection APIs, see Detect and Replace Subsystem Clones Programmatically.

Related Topics