## Control Graph Display

### What You Can Control

MATLAB® allows many figure windows to be open simultaneously during a session. You can control which figures and which axes MATLAB uses to display the result of plotting functions. You can also control to what extent MATLAB clears and resets the properties of the targeted figures and axes.

You can modify the way MATLAB plotting functions behave and you can implement specific behaviors in plotting functions that you write.

Consider these aspects:

• Can you prevent a specific figure or axes from becoming the target for displaying graphs?

• What happens to an existing graph when you plot more data to that graph? Is the existing graph replaced or are new graphics objects added to the existing graph?

### Targeting Specific Figures and Axes

By default, MATLAB plotting functions display graphs in the current figure and current axes (the objects returned by `gcf` and `gca` respectively). You can direct the output to another figure and axes by:

• Explicitly specifying the target axes with the plotting function.

• Making the target axes the current axes.

#### Specify the Target Axes

Suppose you create a figure with two axes, `ax1` and `ax2`.

```tiledlayout(1,2) ax1 = nexttile; ax2 = nexttile;```

Call `plot` with the axes object as the first argument:

`plot(ax1,1:10)`

For plotting functions that do not support the axes first argument, set the `Parent` property:

```t = 0:pi/5:2*pi; patch(sin(t),cos(t),'y','Parent',ax2)```

#### Make the Target Current

To specify a target, you can make a figure the current figure and an axes in that figure the current axes. Plotting functions use the current figure and its current axes by default. If the current figure has no current axes, MATLAB creates one.

If `fig` is the handle to a figure, then the statement

`figure(fig)`
• Makes `fig` the current figure.

• Restacks `fig` to be the front-most figure displayed.

• Makes `fig` visible if it was not (sets the `Visible` property to `'on'`).

• Updates the figure display and processes any pending callbacks.

The same behavior applies to axes. If `ax` is the handle to an axes, then the statement

`axes(ax)`
• Makes `ax` the current axes.

• Restacks `ax` to be the front-most axes displayed.

• Makes `ax` visible if it was not.

• Updates the figure containing the axes and process any pending callbacks.

#### Make Figure or Axes Current Without Changing Other State

You can make a figure or axes current without causing a change in other aspects of the object state. Set the root `CurrentFigure` property or the figure object's `CurrentAxes` property to the handle of the figure or axes that you want to target.

If `fig` is the handle to an existing figure, the statement

```r = groot; r.CurrentFigure = fig;```

makes `fig` the current figure. Similarly, if `ax` is the handle of an axes object, the statement

`fig.CurrentAxes = ax;`

makes it the current axes, if `fig` is the handle of the axes’ parent figure.