How to programmatically detect the difference between a figure and a random dialog or waitbar

I have a case where I am defining a default figure create function. In the create function I want to format figures (intended for plotting on) in a certain fashion such as change the background color and add some annotation text in a couple of places. My current mechanism for detecting the difference between a figure for plotting and any other random dialog or waitbar is to look at the tag for the uicontrol. If the tag is empty that indicates it's a figure for plotting. However what if a user creates a figure and sets the tag for their figure? This scenario busts my logic. I want to define a more robust way to determine if a figure is for plotting or if it's just some other dialog or waitbar.
After some brainstorming with some other colleagues about the issue at hand we have decided to try and compile a comprehensive list of potential dialog and waitbar tags for a range of Matlab versions. Where can I find a comprehensive list for individual versions? If this is not feasible is there a better way to detect the difference other than using the tags?

4 commentaires

Thomas - is it safe to assume that a "figure for plotting" is different from any other random dialog or waitbar if it has an axes? If you know the handle to a figure, you could do
figHandles = findobj(0, 'Type', 'figure');
for k = 1:length(figHandles)
if ~isempty(findobj(figHandles(k), 'Type', 'axes'))
% hFig is figure with an axes
fprintf('%f is a figure with an axes\n', figHandles(k));
else
fprintf('%f is NOT a figure with an axes\n', figHandles(k));
end
end
Perhaps this is something that you are doing already. The use of findobj will ignore any figures whose HandleVisibility is set to "off" or "callback" in my version of MATLAB. For me, this means that the waitbar is ignored.
Figures don't have axes when they are being created. Otherwise that would be a solution. I need to know at figure creation time whether it's a dialog or waitbar. If I can detect that it is one of those then I know it's not a figure for plotting on.
What about checking for the Children of the figure object. At the time of creation, if the figure does not have any object in it, then its Children will also return an empty array, which will mean that the figure was created for plotting.
f % figure handle
if isempty(f.Children)
% plot figure
else
% some other figure
end
The solution I ended up implementing from the context of the figure_CreateFcn is
isDialog = (strcmp(get(src, 'ToolBar'), 'none') || strcmp(get(src, 'ToolBar'), 'auto')) && strcmp(get(src, 'MenuBar') 'none');

Connectez-vous pour commenter.

 Réponse acceptée

Can you use the 'menubar' property? If not, run get(f), get(w), get(d) to compare all properties to see if you can find any unique ID.
%%
f=figure;
w=waitbar(0.5,'please wait');
d=dialog('WindowStyle','normal');
get(f,'menubar')
get(w,'menubar')
get(d,'menubar')
ans =
'figure'
ans =
'none'
ans =
'none'

5 commentaires

That's one of the suggestions someone made and has the fewest corner cases other than compiling a complete list of possible tags.
The menubar can be turned on and off easily.
My current way of hanlding it, which has been the case for a very long time, is to do a "isempty(get(src, 'Tag'))" to see if it's a figure, since figures by defualt have an empty char array for a tag while dialogs and waitbars have content for their tags. This is from the context of the figure_CreateFcn. My problem is if someone has "figure('Tag', 'Hello World')" in their code it would throw my figure_CreateFcn off, tricking it into thinking it was a dialog or waitbar. This would prevent my figures from getting a standard format that my team has decided ALL figures need.
Option 2 would have been to get a list of all known tags for dialogs and waitbars to check the src's tag against to see if it is something other than a figure. This is cumbersome and subject to cornercases that weren't considered.
Option 3 is to check to see if the src's menubar and toolbar are turned off. I cannot think of a case where the analysts I maintain software for will ever turn those bars off while creating a figure for plotting data to.
figure, waitbar and dialog all return Figure object so it not too difficult to hack if someone wants to break your logic on purpose. 'handlevisibility' might be another candicate. It is 'on' for figure and 'callback' for waitbar and dialog.
I ended up just checking to see what the toolbar and menubar properties were set to. There is never a case where someone here wants to plot something and turn off those bars. Likewise I cannot think of any dialog where those are showing. I did discover that the function "dialog" creates a figure with one of the two set to 'auto'.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur App Building dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by