check if figure exists whithout making it appear and using its figure number in findobj()
306 views (last 30 days)
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:
but rather use the number of the figure used when the figure is created, using
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.
More Answers (6)
Jorg Woehl on 20 Nov 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)
dpb on 20 Jul 2013
Most basic is simply (assuming hfig is the handle you're looking for)
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...
Jan on 20 Jul 2013
Simpler and faster than using FINDOBJ:
ishandle(h) && strcmp(get(h, 'type'), 'figure')