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

조회 수: 9 (최근 30일)
Prachi_C
Prachi_C 2018년 1월 1일
댓글: stefano rota 2022년 11월 20일
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.

답변 (2개)

Jonathan Jacobs
Jonathan Jacobs 2018년 9월 17일
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 2021년 6월 24일
In design View of App designer you can select(enable) Single Running Instance Option in code options.
  댓글 수: 1
stefano rota
stefano rota 2022년 11월 20일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by