check if figure exists whithout making it appear and using its figure number in findobj()

245 visualizaciones (últimos 30 días)
Hi,
I want to know if a figure exists, however without it ever appearing at the forefront (I thus cannot use simply the figure() function). The function findobj() is a good candidate to solve this problem, however I do not want to use the 'name' as the argument for the search, i.e. I do not want to do:
findobj('type','figure','name','mytitle')
but rather use the number of the figure used when the figure is created, using
figure(nbrfig)
Does something like the following code exists?
findobj('type','figure','NUMBER OF THE FIGURE',nbrfig)
I tried number, handle, ... instead of 'NUMBER OF THE FIGURE' but nothing works. Need help :) thanks.

Respuesta aceptada

dpb
dpb el 23 de Jul. de 2013
Editada: dpb el 23 de Jul. de 2013
Since the figure number handles are the integer number, all you really need to do then is use
ishandle(n)
for the desired number.
Or, I'm not seeing the problem...???
  4 comentarios
Mohamed Abdelhamid
Mohamed Abdelhamid el 8 de Mzo. de 2021
Editada: Mohamed Abdelhamid el 8 de Mzo. de 2021
I noticed that if the figure hasn't been open before and its handle is stored in the workspace, then this statement would give an error. I suggest using the following:
exist('h') && ishandle('h')
where 'handle' is the handle of this figure that is assigned as follows:
h = figure(1);
Of course 1 can be any number you prefer. This way if the handle doesn't exist in the workspace, MATLAB would simply skip the 'ishandle' check and hence avoid the error.
James Cai
James Cai el 7 de Abr. de 2022
Editada: James Cai el 7 de Abr. de 2022
Should be: exist('h','var') && ishandle(h)

Iniciar sesión para comentar.

Más respuestas (6)

Jorg Woehl
Jorg Woehl el 20 de Nov. de 2015
R2014b and later no longer use numeric handles for graphics objects, so most answers given above do not work. In these cases, the following line of code will yield the figure handle to the desired figure:
handle = findobj(allchild(groot), 'flat', 'type', 'figure', 'number', nbrfig)
  1 comentario
Chris Schierer
Chris Schierer el 12 de Jun. de 2023
I'm not sure if this was broken for a while, and then fixed for backwards compatibility later, but in 2022b "ishandle" works for Figure objects and for legacy numeric integer figure handes.
>> qqq=figure(54)
qqq =
Figure (54) with properties:
Number: 54
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [1 1 1.6028e+03 741.2870]
Units: 'pixels'
Show all properties
>> ishandle(qqq)
ans =
logical
1
>> ishandle(54)
ans =
logical
1

Iniciar sesión para comentar.


dpb
dpb el 20 de Jul. de 2013
Most basic is simply (assuming hfig is the handle you're looking for)
ismember(findall(0,'type','figure'),h)
sorts out from all figures whether the one is or is not valid.
ishandle(h) && findobj(h,'type','figure')==h
looks specifically for the one rather than brute force of findall() w/ the added complication must short-circuit if not valid to avoid error in findobj if passed invalid handle.
Many perturbations of the above ideas are possible, obviously...
  1 comentario
Jan
Jan el 20 de Jul. de 2013
Editada: Jan el 20 de Jul. de 2013
ismember sorts the inputs for a binary search. For the small number of figures and for searching one handle only, ANY is faster. Searching all objects wastes time, because the figures are direct children of the root object. Therefore this is can be much faster, when many figures with many HG-objects are open:
any(findobj(allchild(0), 'flat', 'type', 'figure') == h)

Iniciar sesión para comentar.


Jan
Jan el 20 de Jul. de 2013
Simpler and faster than using FINDOBJ:
ishandle(h) && strcmp(get(h, 'type'), 'figure')

Gaëlle
Gaëlle el 23 de Jul. de 2013
Editada: Gaëlle el 23 de Jul. de 2013
HI, Thank you for your answer, but actually a problem remains. I should have reformulated my question by saying "I want to obtain the handle of a figure without it appearing on the front", especially using the number of the figure rather than the name (so something exactly like findobj but where I could use the number of the figure as argument).

Gaëlle
Gaëlle el 23 de Jul. de 2013
Hi,
I did not know it was possible to simply use the number of the figure as the handle, for example get(120,'Position') would return the position of the figure(120). This is all I neeeded, thank you !
  2 comentarios
dpb
dpb el 23 de Jul. de 2013
Well, for figures, the value of the handle is the same as the number of the figure. They're conceptually different but, the value '1' say will return false if it is anything other than a valid handle but any variable the evaluates to identically 1 will test T if there is, indeed a figure of that number extant.
Similarly for handles for other graphics objects; it's purely a numeric comparison for equality as far as can be determined by operations on the values.
'Preciate if could get an 'accepted' answer just to make the effort seem worth while... :)

Iniciar sesión para comentar.


Evandson Dantas
Evandson Dantas el 13 de Oct. de 2022
Editada: Evandson Dantas el 28 de Nov. de 2022
Try this
h = findall(0,'type','figure','name','figure_name');
if isempty(h)
% Figure doesnt exist
else
% Figure exist
end

Categorías

Más información sobre Graphics Object Identification en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by