How do I create a single instance of MATLAB app (.mlapp file)?

30 visualizaciones (últimos 30 días)
Prachi_C
Prachi_C el 1 de En. de 2018
Comentada: stefano rota el 20 de Nov. de 2022
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.

Respuestas (2)

Jonathan Jacobs
Jonathan Jacobs el 17 de Sept. de 2018
Here's (partially) my skanky hack. I place this in the startup function:
set(0,'ShowHiddenHandles','on')
ch = get(0,'Children');
found=zeros(1,length(ch));
keeper=zeros(1,length(ch));
for i=1:length(ch)
if strcmpi(ch(i).Name, 'EM Data Manager')
found(i)=i;
if strcmpi(ch(i).Tag,'EM Data Manager')
keeper(i)=i;
end
end
end
if length(find(keeper>0)) > 1
disp('Panic! Multiple keeper windows found!')
keyboard
elseif length(find(keeper>0))==1
% control window already exists.
ML_W_switch('mlw')
figure(ch(keeper(keeper>0)))
delete(app.EMDataManagerUIFigure)
%close(app.EMDataManagerUIFigure)
%clear app
app=ch(keeper(keeper>0)).UserData;
% doesn't go up the line back to orig caller.
%assignin('caller','app',app);
else
% create new control window
olddir=pwd;
cd(findomprefs)
scrsize=get(0,'ScreenSize');
if exist('datamgr.mat','file')
load datamgr.mat wpos autofilt
try
app.auto_filt.Value = autofilt;
catch
app.auto_filt.Value = 1;
end
else
wpos = [scrsize(3)/2 scrsize(4)/2];
end
try
cd(olddir)
catch
end
cursize=app.EMDataManagerUIFigure.Position;
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;
end
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.

Nitin Phadkule
Nitin Phadkule el 24 de Jun. de 2021
In design View of App designer you can select(enable) Single Running Instance Option in code options.
  1 comentario
stefano rota
stefano rota el 20 de Nov. de 2022
when calling again the app running in single instance from caller, it seems thar startupfnc in no more precessed in the called app. How can I update input arguments?

Iniciar sesión para comentar.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by