Thread Subject: Gui multiple instances

Subject: Gui multiple instances

From: Jane

Date: 3 Nov, 2009 21:00:20

Message: 1 of 10

Is there a way of preventing the user from opening the same GUI multiple times?

Is there a way of determining that a GUI of that name is already running and hence set the focus to the one already open instead of creating a new version?

Thanks

Subject: Gui multiple instances

From: Matt Fig

Date: 3 Nov, 2009 21:59:04

Message: 2 of 10

Yes, many ways.

One way is to have the first part of your GUI look for a figure with the tag 'MYGUI' and if it doesn't find such a figure, create the GUI (with tag 'MYGUI' of course). If the first code does find a figure with the tag 'MYGUI' it just returns.

Subject: Gui multiple instances

From: Jane

Date: 4 Nov, 2009 18:01:05

Message: 3 of 10

I haven't been able to find a method to search for the figure.

I think perhaps this is bacause I have the 'handlevisibilty' to callback. Is there a way of finding an instance of the GUI with this property set?

In the meantime I have set the 'windowstyle' to modal, which does prevent multiple instances but is far from a perfect solution.

I'ld be grateful of further suggestions

Thanks

Subject: Gui multiple instances

From: someone

Date: 4 Nov, 2009 18:42:04

Message: 4 of 10

"Jane " <j.l.terry@hotmail.co.uk> wrote in message <hcsfh0$7ag$1@fred.mathworks.com>...
> I haven't been able to find a method to search for the figure.
>
> I think perhaps this is bacause I have the 'handlevisibilty' to callback. Is there a way of finding an instance of the GUI with this property set?
>
> In the meantime I have set the 'windowstyle' to modal, which does prevent multiple instances but is far from a perfect solution.
>
> I'ld be grateful of further suggestions
>
> Thanks

You can try setting the 'ShowHiddenHandles' root property to 'on',
do your figure search test (by 'Tag' or whatever),
then set the 'ShowHiddenHandles' root property back to 'off'.

Subject: Gui multiple instances

From: Jane

Date: 4 Nov, 2009 19:24:03

Message: 5 of 10

Thankyou,

I used the following code at the beginning of my GUI function

% Check to see if an instance of GUI is already running -----------------
set(0,'showhiddenhandles','on');
p = findobj(gcf,'tag','uis');
set(0,'showhiddenhandles','off');
if ~isempty(p)
    figure(p);
    msgbox('Simulate Data is already open.');
    return;
end

Which seems to work fine in 2008a, but causes a new figure to be opened on the first run in 7.0.1.

Is this the type of code you meant? And can I prevent a new window opening in 7.0.1?

Subject: Gui multiple instances

From: someone

Date: 4 Nov, 2009 19:52:03

Message: 6 of 10

"Jane " <j.l.terry@hotmail.co.uk> wrote in message <hcskcj$ao9$1@fred.mathworks.com>...
> Thankyou,
>
> I used the following code at the beginning of my GUI function
>
> % Check to see if an instance of GUI is already running -----------------
> set(0,'showhiddenhandles','on');
> p = findobj(gcf,'tag','uis');
> set(0,'showhiddenhandles','off');
> if ~isempty(p)
> figure(p);
> msgbox('Simulate Data is already open.');
> return;
> end
>
> Which seems to work fine in 2008a, but causes a new figure to be opened on the first run in 7.0.1.
>
> Is this the type of code you meant?

Yes

  And can I prevent a new window opening in 7.0.1?

% Maybe instead of:
if ~isempty(p)
% you can try:
if ishandle(p)

% And if you are really paranoid, you can test if p is ONLY a
% figure handle by testing whether the 'Parent' property is root.

Subject: Gui multiple instances

From: Jane

Date: 4 Nov, 2009 20:35:06

Message: 7 of 10

> And can I prevent a new window opening in 7.0.1?
>
> % Maybe instead of:
> if ~isempty(p)
> % you can try:
> if ishandle(p)
>
> % And if you are really paranoid, you can test if p is ONLY a
> % figure handle by testing whether the 'Parent' property is root.

Thanks,
I have changed my condition to ishandle.
The new figure window in 7.0.1 was caused by the way I had findobj. I have now changed this to:

p=findobj('tag','uis','parent',0)

and it seems to be working fine in both versions of Matlab.

Thanks agin for your time, it's much appreciated.

Subject: Gui multiple instances

From: someone

Date: 4 Nov, 2009 20:53:03

Message: 8 of 10

"Jane " <j.l.terry@hotmail.co.uk> wrote in message <hcsohp$4lb$1@fred.mathworks.com>...
> > And can I prevent a new window opening in 7.0.1?
> >
> > % Maybe instead of:
> > if ~isempty(p)
> > % you can try:
> > if ishandle(p)
> >
> > % And if you are really paranoid, you can test if p is ONLY a
> > % figure handle by testing whether the 'Parent' property is root.
>
> Thanks,
> I have changed my condition to ishandle.
> The new figure window in 7.0.1 was caused by the way I had findobj. I have now changed this to:
>
> p=findobj('tag','uis','parent',0)

% Ah, yes!

>
> and it seems to be working fine in both versions of Matlab.
>
> Thanks agin for your time, it's much appreciated.

% BTW, I noticed that ishandle returns a vector, not a scalar.
% So you may want to use something like:

if any(ishandle(p))

doc if

Remarks
Nonscalar Expressions
If the evaluated expression yields a nonscalar value,
then every element of this value must be true or nonzero
for the entire expression to be considered true.

Subject: Gui multiple instances

From: Jane

Date: 4 Nov, 2009 21:16:20

Message: 9 of 10

I've made that change thanks code now looks like:

% Check to see if an instance of GUI is already running -----------------
set(0,'showhiddenhandles','on');
p = findobj('tag','uis','parent',0);
set(0,'showhiddenhandles','off');
if any(ishandle(p))
    figure(p);
    msgbox('Simulate Data is already open.','createMode','modal');
    return;
end

and is working :D

Subject: Gui multiple instances

From: Matt Fig

Date: 5 Nov, 2009 05:55:03

Message: 10 of 10

Just a note, you can use findall instead of having to set the root property for findobj.

Tags for this Thread

Everyone's Tags:

gui

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
gui Jane T 3 Nov, 2009 16:04:03
rssFeed for this Thread

Contact us at files@mathworks.com