by An Uncommon Lab


Creates a figure with the given ID (tag) or selects it if it already exists, allowing one to easily reuse the same figure window identified with text instead of handles. This is useful when, e.g., running a script many times after clearing between runs without having to hard-code figure numbers (which can become hard to keep track of).

h = unique_figure(id, varargin)

Any additional arguments are passed along to the figure's set method.



A unique text name used to refer to the figure


Any additional arguments to pass to the figure's set method



Figure handle


Create a figure with a hidden tag called 'trajectory' and plot something in it. Also, give the figure window the name "Trajectories" and make the background white.

h = unique_figure('trajectory', ...
                  'Name', 'Various Trajectories', ...
                  'Color', 'w');
theta = linspace(0, 2*pi, 1000);
r     = cos(5*theta);
plot(r.*cos(theta),             r.*sin(theta),             'b', ...
     0.8 * r.*sin(theta) + 0.2, 0.8 * r.*cos(theta) + 0.1, 'r');
axis off equal;
% ... Do some more things....

Later on in the script, get the figure back and add to it.

hold on;
plot(0.7 * r.*sin(theta+pi/4) + 0.3, 0.7 * r.*cos(theta+pi/4) - 0.1, ...
     'Color', [0.1 0.8 0.1]);
hold off;

Table of Contents

  1. Inputs
  2. Outputs
  3. Example