How do I get the prior figure handle?

41 ビュー (過去 30 日間)
Avishai Benyamini
Avishai Benyamini 2018 年 7 月 2 日
コメント済み: Walter Roberson 2021 年 7 月 1 日
I have a gui that does data analysis on figures. I want it to act on the figure selected before the gui figure. How do I get the previous figure handle?

回答 (3 件)

Kaitlyn Keil
Kaitlyn Keil 2018 年 7 月 2 日
It looks as though you can call
figHandles = findobj('Type', 'figure');
to get an array of all currently open figures, with the most recently accessed towards the front. If the gui figure is currently selected and the one you want was just before that, you should be able to get it back with
figHandles(2)
Hope that helps!
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 7 月 2 日
This does appear to work if you used figure() to make a figure active -- or code did it for you implicitly, including if you used axes() on a stored axes to make that axes the current axes.
However, this technique does not record which figure recently had "activity" on it that did not involve making the graphic object current. For example,
fig1 = figure('name', 'f1');
ax1 = axes('parent', fig1);
fig2 = figure('name', 'f2');
%at this point fig2 is the active figure
plot(ax1, rand(1,20)); %graphics activity without making ax1 current
%at this point, fig2 is still the active figure

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


Guillaume
Guillaume 2018 年 7 月 2 日
The simplest way to get the prior figure handle is to save that figure handle when you create that prior figure.
hfig = figure; %create figure and save handle.

Rodrigo Bernal
Rodrigo Bernal 2021 年 7 月 1 日
To close the previous figure, for example:
figHandles=findobj('Type','figure');
close(figHandles(end-1));
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 7 月 1 日
The most recent figure is towards the top of the list. The second-most-recent would be figHandles(2)
For example,
figure(1); figure(2); figure(3); figure(4);
figHandles = findobj('Type','figure')
figHandles =
4×1 Figure array: Figure (4) Figure (3) Figure (2) Figure (1)
close(figHandles(end-1))
figHandles
figHandles =
4×1 Figure array: Figure (4) Figure (3) Figure Figure (1)
figure 2 now the one that is gone, but it is not the one before the most recent.

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

カテゴリ

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