rename plot names in a loop

How can I rename my figures in a loop after plotting my data?
I want the them to be called 'Data 1','Data 2','Data 3'.

Answers (1)

Store the handles of the figures in a vector:
FigList = gobjects(1, 3);
for k = 1:3
FigList(k) = figure;
end
% Then the naming is easy (but could be done in the loop above already?!)
for k = 1:3
FigList(k).Name = sprintf('Data %d', k);
end

1 Comment

People often use the term "figure" to refer to either the figure window or to the axes contained in the figure window. Jan's code addresses the first usage. If you have multiple axes and you want to label each with a title, use the title function either right after you create the plot or at the end (using the axes handle.)
x = 0:360;
ax = gobjects(1, 2);
ax(1) = axes;
axis(ax(1), [0 360 -1 1])
plot(ax(1), x, sind(x))
figure
ax(2) = axes;
axis(ax(2), [0 360 -1 1])
plot(ax(2), x, cosd(x))
title(ax(1), "Sine")
title(ax(2), "Cosine")

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 25 Mar 2021

Commented:

on 25 Mar 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!