How do I create a single instance of MATLAB app (.mlapp file)?
26 views (last 30 days)
I am using MATLAB 2017a and creating a GUI using app designer. However every time I run the app it creates a new instance of the GUI. GUIDE has a feature for creating singleton GUI. How do I achieve similar functionality with app designer.
Jonathan Jacobs on 17 Sep 2018
Here's (partially) my skanky hack. I place this in the startup function:
ch = get(0,'Children');
if strcmpi(ch(i).Name, 'EM Data Manager')
if strcmpi(ch(i).Tag,'EM Data Manager')
if length(find(keeper>0)) > 1
disp('Panic! Multiple keeper windows found!')
% control window already exists.
% doesn't go up the line back to orig caller.
% create new control window
load datamgr.mat wpos autofilt
app.auto_filt.Value = autofilt;
app.auto_filt.Value = 1;
wpos = [scrsize(3)/2 scrsize(4)/2];
newsize = [wpos(1:2) cursize(3) cursize(4)];
app.EMDataManagerUIFigure.Position = newsize;
app.loaded_data.UserData = app.f_info;
app.EMDataManagerUIFigure.Tag = 'EM Data Manager';
app.EMDataManagerUIFigure.NextPlot = 'new';
app.EMDataManagerUIFigure.UserData = app;
app.handholder.UserData = app;
Basically, I check to see if there is a previous open instance. If there is, I grab its UserData (where I previously stored the original instance's 'app'). I delete the new instance and continue on using the previous 'app' structure.
It works fine, as long as you don't care about the handle that is returned when you call the mlapp. The NEW 'app' is deleted, and that is that all the middle-man functions between your mlapp's startupFcn and the call to it (either command line or from another function) only remember the NEW app, which was deleted. I tried assignin('caller'... but that only gets as far as the next level up in the caller stack.
Another option might be to regularly save all important values from the original instance and transfer them to the new instance before deleting the old instance.
It's a mess.