Refactor Models to Improve Component Reuse - MATLAB & Simulink (original) (raw)

Main Content

You can use the Metrics Dashboard to identify clones across a model hierarchy. Clones are identical MATLAB Function blocks, identical Stateflow® charts, and subsystems that have identical block types and connections. Clones can have different parameter settings and values. To replace clones with links to library blocks, you can open the Clone Detector app from the Metrics Dashboard.

Use the Clone Detector app to refactor a model, improve model componentization and readability, and reuse components within a model. In this example, you launch the Clone Detector from the Metrics Dashboard. However, you can also open it by opening theApps tab and clicking Clone Detector.

  1. In the Architecture section, the blue bar in the Actual Reuse widget indicates the fraction of total number of subcomponents that are linked library blocks. Pause over theActual Reuse widget to see more information. For this model, 10% of the total number of subcomponents are linked library blocks.
  2. To see more details, click the blue bar. System Lag, Throttle Command, andCheckRange are linked library blocks.
    Table with details on the library linked component content metric results for the sldemo_fuelsys model and its subsystems, charts, and MATLAB Function blocks
  3. Return to the main dashboard page.
  4. In the Architecture section, thePotential Reuse bar indicates that the model contains clones. Pause over Potential Reuse. For this model, 7% of the subcomponents are clones.
  5. To see more details, click the yellow bar.Pressure.map_estimate andThrottle.throttle_estimate are clones of each other.
  6. To determine whether these clones are candidates for replacement with linked library blocks, click Open Conversion Tool.
    The Clone Detector app opens as a new tab in the model.
  7. In the Clone Detection Results and Actions pane, click the Clone Results tab.
    There is one clone group. The light blue shading indicates that these clones are similar clones and not exact clones. Similar clones have different parameter settings and values.
  8. Expand the clone group.
    This clone group consists of two subcharts.
    Clone Detector App showing Clone Detection Results and Actions for the clone group
  9. To determine parameter differences, in the Block Difference column, click View parameter difference.
    The subcharts in this clone group call Simulink® functions that differ only by the value of the breakpoints parameters in the Lookup Table blocks inside of them.
  10. In the Clone Results tab, for theLibrary to place clones parameter, use theBrowse button to choose a library or specify a new library name. If you specify a new library name, the app creates the library.
  11. Save the model to your working folder and, in the Clone Detector tab, click Replace Clones. The app replaces similar clones with links to masked library subsystems, if possible.
    In the Logs tab, click the latest log.
    The log contains a message indicating that the clones cannot be replaced with linked library blocks because the data in the Simulink Functions cannot be promoted to subchart data.
  12. Close the Metrics Dashboard and the model.

When the Clone Detector app refactors a model to replace clones with links to library blocks, the app creates a backup folder. The backup folder name has the prefix m2m_ _<model name>_. If you have aSimulink Test™ license, you can verify the equivalency of the refactored and original models by clicking Check Equivalency in the Clone Detector tab.

Explore Other Options

This table contains a list of common tasks that you can address with Simulink Check™.