주요 콘텐츠

Print Models Programmatically

R2026b

You can print Simulink® model diagrams and export them to image file formats programmatically.

Printing Commands

The MATLAB® print command provides several printing options. For example, suppose you have a model named myModel containing a subsystem named mySubsystem. To print the subsystem in the model to your default printer, enter these commands in the MATLAB Command Window.

open_system("myModel");
print -smySubsystem

When you use the print command, you can print only one diagram. To print multiple levels in a model hierarchy, use multiple print commands, one for each diagram that you want to print.

Tip

Alternatively, consider printing interactively. In the Simulink Toolbar, on the Simulation tab, in the File section, click Print. Then, use the Print Model dialog box in the Simulink Editor to specify the diagrams to print. For details, see Select the Systems to Print.

You can use the set_param function with these parameters to specify printing options for models.

Model Parameters for Printing

Parameter

Description

Values

PaperOrientation

Printing paper orientation.

'portrait' | {'landscape'}

PaperPositionMode

Paper position mode.

  • auto – When printing, Simulink software sizes the diagram to fit the printed page. When exporting a diagram as a graphic image, Simulink software sizes the exported image to be the same size as the diagram's normal size on screen.

  • tiled – Print or export diagram over multiple pages. See Print Diagrams over Multiple Pages for more information.

{'auto'} | 'tiled'

PaperType

Printing paper type.

'usletter' | 'uslegal' | 'a0' | 'a1' | 'a2' | 'a3' | 'a4' | 'a5' | 'b0' | 'b1' | 'b2' | 'b3' | 'b4' | 'b5' | 'arch-A' | 'arch-B' | 'arch-C' | 'arch-D' | 'arch-E' | 'A' | 'B' | 'C' | 'D' | 'E' | 'tabloid'

PaperUnits

Printing paper size units.

'normalized' | {'inches'} | 'centimeters' | 'points'

TiledPaperMargins

Size of the margins associated with each tiled page. Each element in the vector represents a margin at the particular edge.

vector — [left, top, right, bottom]

Print Systems with Multiline Names or Names with Spaces

To print a system whose name appears on multiple lines, assign the name to a variable and use that variable in the print command. For example, suppose you have a model named myModel that contains a subsystem with this name:

my

Subsystem

To print the subsystem, use these commands.

open_system("myModel");
sys = sprintf("myModel/my\nSubsystem");
print(["-s"+sys])

To print a system whose name includes one or more spaces, specify the name as a character vector. For example, to print a subsystem named my Subsystem subsystem in a model named myModel, use these commands.

open_system('myModel');
print(['-smy Subsystem'])

Set Paper Orientation and Type

To set just the paper orientation, use the orient function. Alternatively, set the paper orientation by using set_param function with the PaperOrientation model parameter.

To set the paper type, use the set_param function with the PaperType model parameter. For example, to print to US letter-sized paper, set the paper type to 'usletter'.

Print Diagrams over Multiple Pages

By default, each block diagram is scaled during the printing process so that the diagram fits on a single page. In the case of a large diagram, this automatic scaling can compromise the printed image.

Tiled printing allows you to print even the largest block diagrams without sacrificing clarity and detail. Tiled printing allows you to distribute a block diagram over multiple pages. For example, you can use tiling to divide a model as shown in the figure, with each white box and each gray box representing a separate printed page.

Simulink model overlaid onto a uniform grid of white and gray tiles

Enable Tiled Printing

  1. Use the set_param function to set the PaperPositionMode parameter to tiled.

  2. Use the print command with the -tileall argument.

For example, suppose you have a model named myModel containing a subsystem named mySubsystem. To enable tiled printing for the subsystem, use these commands.

open_system("myModel");
set_param("myModel/mySubsystem",PaperPositionMode="tiled");
print("-smyModel/mySubsystem", "-tileall")

Display Tiled Page Boundaries

To display the page boundaries programmatically, use the set_param function with the model parameter ShowPageBoundaries set to on. For example, suppose you have a model named myModel. To show the page boundaries for this model, use these commands.

open_system("myModel");
set_param("myModel",ShowPageBoundaries="on")

Set Tiled Page Margins

By decreasing the margin sizes, you can increase the printable area of the tiled pages. To specify the margin sizes associated with tiled pages, use the set_param function with the TiledPaperMargins parameter. Each margin is 0.5 inches by default. The value of TiledPaperMargins is a vector that specifies margins in this order: [left top right bottom]. Each element specifies the size of the margin at a particular edge of the page. The value of the PaperUnits parameter determines the units of measurement for the margins.

Specify Range of Tiled Pages to Print

To specify a range of tiled page numbers programmatically, use the print command with the -tileall argument and the -pages argument. Append to -pages a two-element vector that specifies the range.

Note

Simulink uses a row-major scheme to number tiled pages. For example, the first page of the first row is 1, the second page of the first row is 2, and so on.

For example, to print the second, third, and fourth pages of a model named myModel, use these commands.

open_system("myModel");
print("-smyModel","-tileall","-pages[2 4]")

Print Models to Image File Formats

To print your model to an image file format such as .png or .jpeg, use the -device argument with the MATLAB print command. For example, to print a model named myModel to a .png format, use these commands.

open_system("myModel");
print -dpng -smyModel myScreenshot.png

To programmatically export a model into an image format:

  • Call your model in the MATLAB command line.

    model %model is your model name
  • Use the print command to save your model in a .jpeg format.

    print('-smodel','-djpeg','new_name')

By default, the canvas (background) of the exported model matches the color of the model. To use a white or transparent canvas for model files that you export to another file format, set the Simulink Settings > General > Export preference. For more information, see Simulink Settings.

See Also

|

Topics