MATLAB: How to implement singleton behavior for a programmtically created GUI

MATLAB

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).

Best 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;