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:

    addpath(fullfile(docroot,'toolbox','simulink','examples'))
    ex_clone_detection_A
    ex_clone_detection_B
    ex_clone_detection_C
    ex_clone_detection_D
    ex_clone_detection_E
    ex_clone_detection_F

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.

    cloneResults.Clones.Summary
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.

    cloneResults.Clones.CloneGroups(1)
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.

    cloneResults.Clones.CloneGroups(1).Summary
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.

     cloneResults.Clones.CloneGroups(1).CloneList
ans =

  5×1 cell array

    {'ex_clone_detection_A/Subsystem'}
    {'ex_clone_detection_B/Subsystem'}
    {'ex_clone_detection_C/Subsystem'}
    {'ex_clone_detection_D/Subsystem'}
    {'ex_clone_detection_F/Subsystem'}

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

Related Topics