Main Content

Detect and Replace Clones Programmatically in a Loop on Multiple Models

This example shows how to programmatically detect and replace clones on multiple models in a loop by operating on models individually. For more information about Clone Detection APIs, see Detect and Replace Subsystem Clones Programmatically.

This example shows how to detect and replace clones programmatically for five Simulink® models using the library file clones_library as a subsystem reference to replace clones.

1. Open the models:

     ex_detect_clones_A
     ex_detect_clones_B
     ex_detect_clones_C
     ex_detect_clones_D
     ex_detect_clones_E
     clones_library

Save the models and library file in the current working directory.

2. Create an array to add the models to:

     modelList = {};

3. Add the models to the modelList array:

      modelList{end+1,1} = 'ex_detect_clones_A';
      modelList{end+1,1} = 'ex_detect_clones_B';
      modelList{end+1,1} = 'ex_detect_clones_C';
      modelList{end+1,1} = 'ex_detect_clones_D';
      modelList{end+1,1} = 'ex_detect_clones_E';
      modelList{end+1,1} = 'ex_detect_clones_F';

4. Define containers to store Results, ReplacementResults and equivalencyCheck object for the models.

     cloneResultsStorage = containers.Map();
     cloneReplacementStorage = containers.Map();
     equivalencyCheckStorage = containers.Map();

5. Add the library file to the cloneDetectionSettings object created from Settings class.

     libName = 'clones_library';
     cloneDetectionSettings = Simulink.CloneDetection.Settings();
     cloneDetectionSettings = cloneDetectionSettings.addLibraries(libName);

6. Use a loop to cycle through the models using the Simulink.CloneDetection.findClones, Simulink.CloneDetection.replaceClones, and Simulink.CloneDetection.checkEquivalency functions.

     for modelIndex = 1:length(modelList)
          modelName = modelList{modelIndex};
         try
             cloneResults = Simulink.CloneDetection.findClones(modelName, cloneDetectionSettings);
             cloneResultsStorage(modelName) = cloneResults;
             cloneReplacementResults = Simulink.CloneDetection.replaceClones(cloneResults);
             cloneReplacementStorage(modelName) = cloneReplacementResults;
             equivalencyCheckResults = Simulink.CloneDetection.checkEquivalency(cloneReplacementResults);
             equivalencyCheckStorage(modelName) = equivalencyCheckResults;
         catch exception
         end
     end

You can access the results of cloneResultsStorage, cloneReplacementStorage, and equivalencyCheckStorage objects for individual models. For more details, see Detect and Replace Subsystem Clones Programmatically.

Related Topics