How can I implement singleton behavior for a programmtically created GUI?

I am creating a GUI programmatically. However I require the singleton GUI behavior for my project. (i.e. if the user tries to run a second instance of my program it only brings the current singleton into focus and does not destroy any objects).

 Accepted Answer

The singleton behavior can be implemented by querying for the tag of the GUI under the root object. If the GUI has been launched , the GUI will be a child of the root object. If the GUI has not been launched, it will not be found and the GUI can be launched.
The following code demonstrates this
function my_gui()
%Search if the figure exists
h = findall(0,'tag','my_gui');
if (isempty(h))
%Launch the figure
figHandle = figure('tag','my_gui');
plot(rand(4,4));
set(figHandle,'handlevisibility','callback');
else
%Figure exists so bring Figure to the focus
figure(h);
end;

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!