Can figure numbers be re-assigned?

25 ビュー (過去 30 日間)
K E
K E 2014 年 6 月 9 日
コメント済み: Pedro 2025 年 10 月 12 日
Is it possible to re-assign a figure number , for example if I have a Figure 55 open, can I make it Figure 2 as long as I don't already have a Figure 2 open? If so how do I do this?
I have a routine that generates 60+ figures, and I delete all but 3 interesting ones. I would like to make them Figures 1,2,3 because I need to re-run the routine and generate another set of figures, and I'd like the first set to appear together in the figure order (it makes them easier to keep track of).
  1 件のコメント
Matt J
Matt J 2024 年 2 月 13 日
編集済み: Matt J 2024 年 2 月 13 日
No perfect solutions yet, unfortunately. I really hope TMW implements this capability. It seems like such a basic thing to want to do.

サインインしてコメントする。

採用された回答

José-Luis
José-Luis 2014 年 6 月 9 日
編集済み: José-Luis 2014 年 6 月 9 日
To get a list of all the existing figures:
get(0,'Children');
If figure two does not exist and you want to copy figure(55):
figure(2)
copyobj(allchild(55),2);
Get rid of figure(55):
clf(55);
And to answer your question directly. I believe once a figure number is set, you have to live with it. But there are workarounds, such as the one above.
  1 件のコメント
K E
K E 2014 年 6 月 9 日
編集済み: K E 2014 年 6 月 9 日
Perfect. Here is how I modified your code to automatically reassign all open figure numbers - I know that's not what my question asked, but perhaps it will help someone else.
% Get a list of all existing figures
figList = sort(get(0,'Children'));
for iOld = 1:length(figList)
figOld = figList(iOld); % This is a figure to replace
% Used later to make sure size of figure is correct
posOld = get(figOld, 'pos');
figNew = figure; % Make new figure
% Used later to make figure appear at
% default horizontal position on screen
posNew = get(figNew, 'pos');
% Copy over lines (but not formatting)
copyobj(allchild(figList(iOld)),figNew);
% Reproduce figure name and size however don't move it horizontally
set(figNew, 'Name', get(figOld, 'Name'), ...
'pos', [posNew(1:2) posOld(3:4)]);
% Get rid of old figure
close(figList(iOld));
end

サインインしてコメントする。

その他の回答 (2 件)

Joseph Cheng
Joseph Cheng 2014 年 6 月 9 日
編集済み: Joseph Cheng 2014 年 6 月 9 日
This maybe a bit hard to implemented based on how you're selecting the 3 interesting ones but the function copyobj() will do the trick.
x = randi(10,1,5);
h1 = figure(1),plot(x);
figure(100);
h2 = gcf;
copyobj(allchild(h1),h2);
Here i create a random plot in figure 1. and then using gcf i get figure 100 and copy figure 1 to figure 100.

Matt J
Matt J 2024 年 2 月 13 日
編集済み: Matt J 2024 年 2 月 13 日
The only reliable way that I have found, one which avoids copyobj and keeps all the figure formatting, is to save all open figures to a temporary .fig file and then reload i. The reloaded figures will always be numbered sequentially starting at the first available fig number.
H=findobj(groot,'Type','figure'); %Get all open figures
tn=tempname('.')+".fig"; %generate temporary name
savefig(H,tn)
close all
openfig(tn); delete(tn);
  1 件のコメント
Pedro
Pedro 2025 年 10 月 12 日
Excellent! Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by