Main Content

Class Diagram Viewer

Create diagrams to investigate class details and hierarchies

Description

The Class Diagram Viewer enables you to create class diagrams that show implementation details and hierarchies. You can use this tool to explore class structures and share class diagrams with others. These diagrams can include details about:

  • The internal structure of classes, including properties and methods and their access attributes

  • The hierarchy of classes, including packages, with inheritance relationships shown graphically

You can create class diagrams using the graphical interface or the command line API defined by matlab.diagram.ClassViewer.

Class Diagram Viewer tool

Open the Class Diagram Viewer

  • MATLAB® command prompt: matlab.diagram.ClassViewer

  • On the Project tab, click the tool icon

Examples

expand all

Open the Class Diagram Viewer. In the Class Browser pane, expand the add icon plus menu icon and select Add Package. Enter matlab.unittest as the package name and click OK. The folder for unittest appears in the Class Browser.

Class Browser with unittest added

Expand the unittest folder and select the InteractiveTestCase class. To do this, click the Add button in the toolbar to add a card for the class to the Class Diagram Viewer canvas. Add the TestCase class using the same method, and click Auto Arrange. Because TestCase is the superclass of InteractiveTestCase, Class Diagram Viewer automatically draws an arrow from InteractiveTestCase to TestCase to show this relationship.

TestCase and InteractiveTestCase classes

The striped blue borders at the top of the class cards indicate that both of these classes are handle classes. The Legend pane describes what the graphic effects and icons on the canvas represent.

Legned pane

You can also drag and drop items from the Class Browser directly to the canvas. Drag and drop the Test class to the viewer. The green plus button button indicates that Test has a viewable superclass.

Test card with green plus button

Click the button to add the superclass to the diagram. The arrow pointing to TestSuite from Test indicates that TestSuite is a superclass of Test.

Test and TestSuite class icons

In addition to moving the classes in the viewer by dragging and dropping, you can customize the view using the options in the Navigate section of the toolbar, including:

  • Zoom in and out

  • Fit the model to current screen size

  • Pan

In the Diagram section, select the Mixins check box. This action does not automatically add mixin classes to the diagram, but it includes them in the display when you perform any action that adds superclasses.

To add the immediate superclass of Test, right-click the Test class card and select Superclass. Click Auto Arrange to clean up the diagram. The CustomDisplay mixin now appears as a superclass of Test. The name of CustomDisplay is italicized, indicating that it is an abstract class.

class diagram with CustomDisplay mixin visible

In addition to relationships between classes, you can also explore the class definitions themselves. Expand the Test class card by clicking the arrow next to the class name in its card. The class card expands to show the properties and methods defined by the class. Properties and methods inherited from superclasses do not appear in the subclass cards.

The icons next to the property and method names also identify the access levels of the properties and methods. For example, the lock icon shows that TestCaseProvider is a private property.

Test class with properties and methods visible

Selecting a property or method on the class card also brings up access and other information in the Inspector pane.

To view the source code of one or more classes, select their cards and click Go To Source in the toolbar. The source code files open in the MATLAB editor.

To save or share the diagram you create, use one of two options:

  • Click Export to save the diagram as a static image.

  • Select Save > Save as... to create an MLDATX file that can be reopened in a Class Diagram Viewer instance and edited.

Programmatic Use

expand all

matlab.diagram.ClassViewer opens a Class Diagram Viewer instance with no classes loaded.

matlab.diagram.ClassViewer(Name,Value) adds the classes specified to both the Class Browser pane and the canvas. Valid name-value arguments include:

  • Classes, with class names specified as strings or object names

  • Folders, with folder names specified as strings

  • Packages, with package names specified as strings

See matlab.diagram.ClassViewer for additional name-value options. Specify arguments as 'Name1',Value1,...,'NameN',ValueN, where Name is the argument name and Value is the corresponding value.

Tips

  • Class Diagram Viewers are instances of the matlab.diagram.ClassViewer class. Use the class constructors to open more than one viewer at a time.

  • When the green plus button button appears on an arrow between two classes, rather than on a class card itself, there are additional classes in the hierarchy between the two connected classes. Click the green plus button button to add all of the viewable classes in that part of the hierarchy.

  • If you make changes to the class code while using the Class Diagram Viewer, you can automatically update the diagram by clicking Refresh.

Introduced in R2021a