Main Content

Configure Model Layout Programmatically

You can programmatically arrange the blocks, signal lines, and annotations in a model, or you can specify the positions of individual blocks. You can also resize, flip, and rotate blocks. The model, library, or subsystem whose layout you want to configure must be loaded. For information about how to load models, libraries, and subsystems, see Create, Load, Open, Save, and Close Models Programmatically.

To learn how to group blocks into subsystems, see Group Blocks into Subsystems Programmatically. For information about how to change model style elements such as the colors and fonts, see Configure Model Style Elements Programmatically.

Automatically Arrange Model Diagram

To improve the arrangement of your model diagram, use the Simulink.BlockDiagram.arrangeSystem and Simulink.BlockDiagram.routeLine functions. The functions route signal lines to avoid overlap with blocks, annotations, and other signal lines. The table shows how to take these actions. For information about how to the handles of models and signal lines, see Get Handles and Paths.

On the left is a model diagram with chaotic overlapping signal lines and blocks, and on the right is the same diagram arranged neatly, with no overlap.

ActionCommand
Improve layout of model diagram with the handle h. This is the equivalent of the Auto Arrange button on the Format tab of the Simulink® Toolstrip.
Simulink.BlockDiagram.arrangeSystem(h)
Improve layout of a set of signal lines. The line handles are stored in the vector h.
Simulink.BlockDiagram.routeLine(h)
When using a script to edit models, defer making changes to signal line routing until the script finishes running.

Before you run the script, run this command to defer line updates for all models.

set_param(0,'LineUpdate','deferred');

When the script finishes, run this command.

Simulink.BlockDiagram.routeLine('DeferredLines')

Position Blocks and Annotations

The position of a block or annotation is determined by the Position parameter. The parameter value is a vector with four elements that respectively define the position left, right, top, and bottom edges of the block or annotation in the Simulink coordinate system.

p = [left right top bottom]

When a model first opens, the origin of the coordinate system is in the upper left corner of the model. The x-axis extends to the right, and the y-axis extends downward. The position of a block or annotation edge can have a positive or negative value, depending on where the edge is located relative to the origin of the coordinate system.

The top left edge of the model canvas showing how each element of the p vector is measured on a Sine Wave block.

You can view and edit the value of the Position parameter of a block or annotation using the get_param and set_param functions, respectively. The table shows examples of how to view and change the position of a block with the handle h. You can view and change the position of an annotation with the handle h using the same syntaxes. For information about how to get handles, see Get Handles and Paths.

ActionCommand
Get the current position of the block.
p = get_param(h,"Position")

Move the block left by three times its width.

A Sine Wave block before and after moving three times its width to the left.

Get the current position of the block.

p = get_param(h,"Position");

Get the distance you want to move the block.

f = 3;
width = p(3)-p(1);
delta = f*width;

Change the p vector to reflect the new position.

p = [p(1)-delta p(2) p(3)-delta p(4)];

Update the Position property of the block with the new p vector.

set_param(h,Position=p);

Move the block right by three times its width.

A Sine Wave block before and after moving three times its width to the right.

p = get_param(h,"Position");
f = 3;
width = p(3)-p(1);
delta = f*width;
p = [p(1)+delta p(2) p(3)+delta p(4)];
set_param(h,Position=p);

Move the block up by three times its height.

A Sine Wave block before and after moving up by three times its height.

p = get_param(h,"Position");
f = 3;
height = p(4)-p(2);
delta = f*height;
p = [p(1) p(2)-delta p(3) p(4)-delta];
set_param(h,Position=p);

Move the block down by three times its height.

A Sine Wave block before and after moving down by three times its height.

p = get_param(h,"Position");
f = 3;
height = p(4)-p(2);
delta = f*height;
p = [p(1) p(2)+delta p(3) p(4)+delta];
set_param(h,Position=p);

You can also view and edit the position of an annotation using the name of the Simulink.Annotation object and dot notation. The table shows two examples. For information about how to get a Simulink.annotation object, see Get Handles and Paths.

ActionCommand
Get the current position of the annotation whose Simulink.annotation object is stored in the variable named a.
a.Position

Set the position of the annotation whose Simulink.annotation object is stored in the variable named a to [50 0 134 15].

a.Position = [50 0 134 15];

By changing the position of a block or annotation, you can line up blocks and annotations. The table shows examples of how to line up two blocks with the handles h1 and h2. In the examples, the block with the handle h1 stays in place, and the block with the handle h2 moves.

ActionCommand

Line up the blocks horizontally at their left edge.

A Product block and a To Workspace block are aligned at their left edges.

Get the current positions of the blocks.

p1 = get_param(h1,"Position");
p2 = get_param(h2,"Position");

Get the distance between the left edges of the two blocks.

delta = p2(1)-p1(1);

Change the p vector to reflect the new position.

p2 = [p2(1)-delta p2(2) p2(3)-delta p2(4)];

Update the Position property of the block with the new p vector.

set_param(h2,Position=p2);

Line up the blocks at their horizontal middle.

A Product block and a To Workspace block are aligned at their horizontal middle.

p1 = get_param(h1,"Position");
p2 = get_param(h2,"Position");
delta = (p2(1)-p1(1)+p2(3)-p1(3))/2;
p2 = [p2(1)-delta p2(2) p2(3)-delta p2(4)];
set_param(h2,Position=p2);

Line up the blocks horizontally at their right edge.

A Product block and a To Workspace block are aligned at their right edges.

p1 = get_param(h1,"Position");
p2 = get_param(h2,"Position");
delta = p2(3)-p1(3);
p2 = [p2(1)-delta p2(2) p2(3)-delta p2(4)];
set_param(h2,Position=p2);

Line up the blocks vertically at their top edge.

A Product block and a To Workspace block are aligned at their top edges.

p1 = get_param(h1,"Position");
p2 = get_param(h2,"Position");
delta = p2(2)-p1(2);
p2 = [p2(1) p2(2)-delta p2(3) p2(4)-delta];
set_param(h2,Position=p2);

Line up the blocks at their vertical middle.

A Product block and a To Workspace block are aligned at their vertical middle.

p1 = get_param(h1,"Position");
p2 = get_param(h2,"Position");
delta = (p2(2)-p1(2)+p2(4)-p1(4))/2;
p2 = [p2(1) p2(2)-delta p2(3) p2(4)-delta];
set_param(h2,Position=p2);

Line up the blocks at their bottom edge.

A Product block and a To Workspace block are aligned at their bottom edges.

p1 = get_param(h1,"Position");
p2 = get_param(h2,"Position");
delta = p2(4)-p1(4);
p2 = [p2(1) p2(2)-delta p2(3) p2(4)-delta];
set_param(h2,Position=p2);

Line up the blocks at their center.

A Product block and a To Workspace block are aligned at their centers.

p1 = get_param(h1,"Position");
p2 = get_param(h2,"Position");
deltaH = (p2(1)-p1(1)+p2(3)-p1(3))/2;
deltaV = (p2(2)-p1(2)+p2(4)-p1(4))/2;
p2 = [p2(1)-deltaH p2(2)-deltaV p2(3)-deltaH p2(4)-deltaV];
set_param(h2,Position=p2);

Resize Blocks

You can resize a block by changing the value of the Position parameter. To change the block width, change the position of the right edge of the block relative to the left edge of the block. To change the block height, change the position of the bottom edge of the block relative to the top edge of the block. You can view and edit the value of the Position parameter using the get_param and set_param functions, respectively. The table shows examples of how to view and change the size of a block with the handle h. For information about how to get block handles, see Get Handles and Paths.

ActionCommand
Get the current width of the block.
p = get_param(h,"Position");
width = p(3)-p(1)
Get the current height of the block.
p = get_param(h,"Position");
height = p(4)-p(2)

Stretch the block horizontally to the left by a factor of 3.

A Sine Wave block before and after stretching to the left by a factor of three.

Specify the factor by which you want to stretch the block.

f = 3;

Get the current position of the block.

p = get_param(h,"Position");

Get the distance by which you want to stretch the block.

width = p(3)-p(1);
delta = (f-1)*width;

Change the p vector to reflect the new position of the right edge of the block.

p = [p(1)-delta p(2) p(3) p(4)];

Update the Position property of the block with the new p vector.

set_param(h,Position=p);

Stretch the block horizontally from its center by a factor of 3.

A Sine Wave block before and after stretching about its horizontal middle by a factor of three.

f = 3;
p = get_param(h,"Position");
width = p(3)-p(1);
delta = ((f-1)*width)/2;
p = [p(1)-delta p(2) p(3)+delta p(4)];
set_param(h,Position=p);

Stretch the block horizontally to the right by a factor of 3.

A Sine Wave block before and after stretching to the right by a factor of three.

f = 3;
p = get_param(h,"Position");
width = p(3)-p(1);
delta = (f-1)*width;
p = [p(1) p(2) p(3)+delta p(4)];
set_param(h,Position=p);

Contract the block horizontally to the left by a factor of 3.

A Sine Wave block before and after contracting to the left by a factor of three.

f = 1/3;
p = get_param(h,"Position");
width = p(3)-p(1);
delta = (f-1)*width;
p = [p(1) p(2) p(3)+delta p(4)];
set_param(h,Position=p);

Contract the block horizontally from its center by a factor of 3.

A Sine Wave block before and after contracting about its horizontal middle by a factor of three.

f = 1/3;
p = get_param(h,"Position");
width = p(3)-p(1);
delta = ((f-1)*width)/2;
p = [p(1)-delta p(2) p(3)+delta p(4)];
set_param(h,Position=p);

Contract the block horizontally to the right by a factor of 3.

A Sine Wave block before and after contracting to the right by a factor of three.

f = 1/3;
p = get_param(h,"Position");
width = p(3)-p(1);
delta = (f-1)*width;
p = [p(1)-delta p(2) p(3) p(4)];
set_param(h,Position=p);

Stretch the block vertically upward by a factor of 3.

A Sine Wave block before and after stretching up by a factor of three.

f = 3;
p = get_param(h,"Position");
height = p(4)-p(2);
delta = (f-1)*height;
p = [p(1) p(2)-delta p(3) p(4)];
set_param(h,Position=p);

Stretch the block vertically from its middle by a factor of 3.

A Sine Wave block before and after stretching about its vertical middle by a factor of three.

f = 3;
p = get_param(h,"Position");
height = p(4)-p(2);
delta = (f-1)*height/2;
p = [p(1) p(2)-delta p(3) p(4)+delta];
set_param(h,Position=p);

Stretch the block vertically downward by a factor of 3.

A Sine Wave block before and after stretching down by a factor of three.

f = 3;
p = get_param(h,"Position");
height = p(4)-p(2);
delta = (f-1)*height;
p = [p(1) p(2) p(3) p(4)+delta];
set_param(h,Position=p);

Contract the block vertically upward by a factor of 3.

A Sine Wave block before and after contracting up by a factor of three.

f = 1/3;
p = get_param(h,"Position");
height = p(4)-p(2);
delta = (f-1)*height;
p = [p(1) p(2)-delta p(3) p(4)];
set_param(h,Position=p);

Contract the block vertically from its middle by a factor of 3.

A Sine Wave block before and after contracting about its vertical middle by a factor of three.

f = 1/3;
p = get_param(h,"Position");
height = p(4)-p(2);
delta = (f-1)*height/2;
p = [p(1) p(2)-delta p(3) p(4)+delta];
set_param(h,Position=p);

Contract the block vertically downward by a factor of 3.

A Sine Wave block before and after contracting down by a factor of three.

f = 1/3;
p = get_param(h,"Position");
height = p(4)-p(2);
delta = (f-1)*height;
p = [p(1) p(2) p(3) p(4)+delta];
set_param(h,Position=p);

Stretch the block horizontally and vertically from its center by a factor of 3.

A Sine Wave block before and after stretching about its center by a factor of three.

f = 3;
p = get_param(h,"Position");
width = p(3)-p(1);
height = p(4)-p(2);
delta1 = (f-1)*width/2;
delta2 = (f-1)*height/2;
p = [p(1)-delta1 p(2)-delta2 p(3)+delta1 p(4)+delta2];
set_param(h,Position=p);

Contract the block horizontally and vertically from its center by a factor of 3.

A Sine Wave block before and after contracting about its center by a factor of three.

f = 1/3;
p = get_param(h,"Position");
width = p(3)-p(1);
height = p(4)-p(2);
delta1 = (f-1)*width/2;
delta2 = (f-1)*height/2;
p = [p(1)-delta1 p(2)-delta2 p(3)+delta1 p(4)+delta2];
set_param(h,Position=p);

These blocks display a parameter value on their block icons:

When the blocks are too small to display the full parameter value, the blocks display a placeholder letter instead, for example, -K-. To resize blocks of these types to be large enough to display their values, use the Simulink.BlockDiagram.resizeBlocksToFitContent function. The table shows examples of how to use the function. The function does not act on blocks in referenced models.

On the left is a default Gain block displaying -K-, and on the right is a larger Gain block displaying 1000

ActionCommand

Resize all blocks of the listed block types in a model named myModel to be large enough to display their values.

Simulink.BlockDiagram.resizeBlocksToFitContent("myModel")
Get the paths of all blocks of the listed block types in a model named myModel that display placeholder letters. Do not resize the blocks.
blocks = Simulink.BlockDiagram.resizeBlocksToFitContent("myModel",...
ReportOnly="true")

A model named myModel contains a subsystem named mySubsystem. Resize all blocks of the listed block types that are in the subsystem to be large enough to display their values.

blocks = find_system("myModel/mySubsystem");
Simulink.BlockDiagram.resizeBlocksToFitContent("myModel",...
BlockList=blocks)

Flip Blocks

The orientation of a block is determined by the Orientation parameter of the block. You can view and edit the value of the Orientation parameter using the get_param and set_param functions, respectively. The table shows examples of how to flip a block with the handle h. For information about how to get block handles, see Get Handles and Paths.

Note

Flipping a block does not flip the block icon. The flip operation described in this section is the operation you can perform interactively using the options on the Format tab of the model window. The final location of the block label might differ between the interactive and programmatic approach.

ActionCommand
Get the current orientation of the block.
get_param(h,"Orientation")

Flip the block left.

A Sine Wave block before and after flipping left.

set_param(h,Orientation="Left");

Flip the block right. Flipping the block right returns the block to its default orientation.

A Sine Wave block before and after flipping right.

set_param(h,Orientation="Right");

Flip the block up.

A Sine Wave block that has been rotated 90 degrees clockwise before and after flipping up.

set_param(h,Orientation="Up");

Flip the block down.

A Sine Wave block that has been rotated 90 degrees clockwise before and after flipping down.

set_param(h,Orientation="Down");

Rotate Blocks

The orientation of a block is determined by the Orientation parameter of the block. You can view and edit the value of the Orientation parameter using the get_param and set_param functions, respectively. The table shows examples of how to rotate a block with the handle h. For information about how to get block handles, see Get Handles and Paths.

Note

Rotating a block does not rotate the block icon. The rotate operation described in this section is the same as the operation you can perform interactively using the options on the Format tab of the model window.

ActionCommand

Get the current orientation of the block.

get_param(h,"Orientation")

Rotate the block 90 degrees clockwise.

A Sine Wave block before and after rotating 90 degrees clockwise.

set_param(h,Orientation="Down");

Rotate the block 180 degrees clockwise.

A Sine Wave block before and after rotating 180 degrees clockwise.

set_param(h,Orientation="Left");

Rotate the block 270 degrees clockwise.

A Sine Wave block before and after rotating 270 degrees clockwise.

set_param(h,Orientation="Up");

Return the block to its default orientation.

A Sine Wave block before and after rotating from 270 degrees clockwise back to its default orientation.

set_param(h,Orientation="Right");

Position, Resize, Flip, or Rotate Multiple Blocks at Once

To position, resize, flip or rotate multiple blocks at once, take the same basic approach as for one block, but specify the blocks as a vector of handles or an array of block paths expressed as a string array or a cell array of character vectors (strings are recommended over character vectors). When specifying multiple blocks, the output of the get_param function is a cell array. Adjust the commands to work with the cell array.

For example, to stretch two blocks with the handles h1 and h2 horizontally and vertically about their centers by a factor of 3, follow these steps.

  1. Create a vector that stores the block handles.

    h = [h1 h2];
  2. Specify the factor by which you want to stretch the blocks.

    f = 3;
  3. Get the current positions of the blocks. The output of the get_param function is a cell array. Convert the cell array to a numerical matrix using the cell2mat function.

    p = cell2mat(get_param(h,"Position"));
  4. Get the distance by which you want to stretch each block. The top row of the matrix stores the position vector of one block. The bottom row stores the position vector of the other block.

    width = p(:,3)-p(:,1);
    height = p(:,4)-p(:,2);
    delta1 = (f-1)*width/2;
    delta2 = (f-1)*height/2;
  5. Change the p matrix to reflect the new block sizes.

    p = [p(:,1)-delta1 p(:,2)-delta2 p(:,3)+delta1 p(:,4)+delta2];
  6. Create a function that takes an index, i, and resizes the block whose handle is stored in the ith row of the h vector.

    myFun = @(i) set_param(h(i),Position=p(i,:));
  7. Create a vector of the indices of all rows in the h vector.

    i = 1:length(h);
  8. Run the function for all indices in the vector i using the arrayfun function.

    arrayfun(myFun,i);

The table shows how the commands for stretching one block horizontally and vertically by a factor of 3 compare to the commands for stretching multiple blocks.

Stretch One BlockStretch Multiple Blocks

h is a block handle.

f = 3;
p = get_param(h,"Position");
width = p(3)-p(1);
height = p(4)-p(2);
delta1 = (f-1)*width/2;
delta2 = (f-1)*height/2;
p = [p(1)-delta1 p(2)-delta2 p(3)+delta1 p(4)+delta2];
set_param(h,Position=p);

A Sine Wave block before and after stretching about its center by a factor of three.

h is a vector of block handles.

f = 3;
p = cell2mat(get_param(h,"Position"));
width = p(:,3)-p(:,1);
height = p(:,4)-p(:,2);
delta1 = (f-1)*width/2;
delta2 = (f-1)*height/2;
p = [p(:,1)-delta1 p(:,2)-delta2 p(:,3)+delta1 p(:,4)+delta2];
myFun = @(i) set_param(h(i),Position=p(i,:));
i = 1:length(h);
arrayfun(myFun,i);

Three Sine Wave blocks before and after stretching about their center by a factor of three.

See Also

| | | |

Topics