Main Content

Find Clones Anywhere in a 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 in your model.

  • Subsystem clones: Identifies clones only in a subsystems.

  • Clones anywhere: Identifies clones anywhere in the model.

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

Identify Clones by Using the App

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

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

  2. Save the model in the current working directory.

  3. On the Apps tab, click Clone Detector. Alternatively, at the MATLAB command line, enter:

    clonedetection("mClonesAnywhereSimilar")
  4. 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.

  5. Click Find Clones to identify clones.

  6. 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.

  7. 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.

    Note

    Only Exact clone refactoring is supported.

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 anywhere in 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('mClonesAnywhereSimilar',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