Main Content

Detect Clones Programmatically on Multiple Models Across Different Folders

This example shows how to programmatically detect clones across multiple models located in different folders. For more information about the clone detection APIs, see Detect and Replace Subsystem Clones Programmatically.

This example demonstrates how to use the clone detection APIs to identify clones in six Simulink® models present in a folder. NOTE: The Clone Detector APIs do not support replacing clones in multiple models.

1. At the MATLAB® command line, enter:


Save the models to a writeable folder.

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

     cloneDetectionSettings = Simulink.CloneDetection.Settings();

3. Add the path of the folder with the models to the cloneDetectionSettings object.

     cloneDetectionSettings.Folders = {'D:\models'}
cloneDetectionSettings = 

  Settings with properties:

                            IgnoreSignalName: 0
                         IgnoreBlockProperty: 0
                      ExcludeModelReferences: 0
                         ExcludeLibraryLinks: 0
                      ExcludeInactiveRegions: 0
                     DetectClonesAcrossModel: 0
              FindClonesRecursivelyInFolders: 1
                    ParamDifferenceThreshold: 50
    ReplaceExactClonesWithSubsystemReference: 0
                                   Libraries: {}
                                     Folders: {'D:\models'}

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

    cloneResults = Simulink.CloneDetection.findClones(cloneDetectionSettings)
cloneResults = 

  Results with properties:

          Clones: [1×1 struct]
    ExceptionLog: ''

The cloneResults is an object of Simulink.CloneDetection.Results class which has two properties, Clones and ExceptionLog.

5. View the Clones.Summary field.

ans = 

  struct with fields:

                 CloneGroups: 4
               SimilarClones: 18
                 ExactClones: 8
                      Clones: 26
    PotentialReusePercentage: [1×1 struct]

In this example, the models have four different clone groups with matching subsystem patterns, eighteen similar clones, and eight exact clones, and the twenty-six subsystem clones.

5. View the details of first clone group.

ans = 

  struct with fields:

         Name: 'Exact Clone Group 1'
      Summary: [1×1 struct]
    CloneList: {5×1 cell}

6. View the summary of first clone group.

ans = 

  struct with fields:

    ParameterDifferences: [1×1 struct]
                  Clones: 5
          BlocksPerClone: 5
               CloneType: 'Exact'
         BlockDifference: 0

7. View the clone list of the first clone group.

ans =

  5×1 cell array


Similarly, you can find the results of other clone groups using the above steps.

Related Topics