Identify and Replace Clones using Model Libraries
Clones are modeling patterns that have identical block types and connections. By using the Clone Detector app, you can find clones and replace them with links to library blocks. This facilitates the reuse of components from model libraries, making it easier to update models. You can baseline modeling patterns in the library and use Clone Detector to replace all instances of these modeling patterns in your model with links to the library blocks.
You can also use the Clone Detector app to create new library blocks for recurring clone patterns in your model if they are not already present in the library, and then replace the clones with links to these new library blocks. For more information, see Enable Component Reuse by Using Clone Detection.
Identify Clones Using External Library
This example shows how to use the Clone Detector app to identify clones and then replace them with links to external library blocks.
Detect Clones
Open the model
ex_detect_clones
.openExample("ex_detect_clones")
Save the model to your working folder. The model must be open to access the app.
On the Apps tab, select Clone Detector from the Apps gallery. Alternatively, on the MATLAB® command line enter:
clonedetection("ex_detect_clones")
Open the library
ex_clone_library
. At the MATLAB command line, enter:openExample("ex_clone_library")
In the Clone Detector tab, select Settings > Match Patterns from Libraries.
Add the library file,
ex_clone_library.slx
.You can also provide path of the folder containing the libraries, and Clone Detector will select all the library files in that folder.
Click Find Clones to identify clones.
In the Clone Detection Results and Actions panel, on the Clone Results tab, a list of clone groups is displayed.
Click the
>
next toex_clone_library/libsubsystem
and select Clone Region 2. SubsystemSS2
is highlighted in the model, indicating the clone.In the Refactor Benefits pane, you can evaluate number of exact clones and similar clones as a percentage of the total number of blocks in the model.
Replace Clones
Click Replace Clones. During the refactoring process, only exact clones are replaced with library links. Similar clones are not linked to the library unless they are refined to match exactly.
You can restore the model to its original state by clicking the
Restore button in the Clone Results
tab of the Clone Detection Results and Actions pane. The Clone
Detector app creates a backup folder in the working directory. The backup folder
name has the prefix m2m_model name
. The
clone data is saved in a MAT file. You can also find a backup of the original model
in the backup folder after refactoring the model.
Identify Clones Programmatically
You can also use MATLAB commands to identify clones in your model from the library. For more details see, Detect and Replace Subsystem Clones Programmatically.
Create a
Simulink.CloneDetection.Settings
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
To search for clones using the library file, provide the library path in the
Libraries
property.cloneDetectionSettings.Libraries = "C:\Project\Libraries\ex_clone_library.slx"
cloneDetectionSettings = IgnoreSignalName: 0 IgnoreBlockProperty: 0 ExcludeModelReferences: 0 ExcludeLibraryLinks: 0 SelectedSystemBoundary: [] FindClonesRecursivelyInFolders: 1 ParamDifferenceThreshold: 50 ReplaceExactClonesWithSubsystemReference: 0 Libraries: {'C:\Project\Libraries\ex_clone_library.slx'} Folders: {} DetectClonesAcrossModel: 0 ExcludeInactiveRegions: 0
To find clones, use the
Simulink.CloneDetection.findClones
function with thecloneDetectionSettings
object.cloneResults = Simulink.CloneDetection.findClones("ex_detect_clones",cloneDetectionSettings)
cloneResults = Clones: [1×1 struct] ExceptionLog: ''
For more details on analyzing the clone detection results, see Programmatically Identify Clones in a Model.