restart an app with code
이전 댓글 표시
Hi,
I am making an app using App Designer. Is there any way to close and re-open (reboot) the app with a pushbottun callback in the app?
Any advise would be appreciated. Thank you!
댓글 수: 1
Guillaume
2019년 6월 30일
No that's not possible, but that sounds like a very strange thing to want to do. Can you describe your use case so we can suggest a better solution.
답변 (3개)
I agree with Guillaume that closing and reopening an app might be a red flag that something is being done suboptimally (or worse). If you're trying to reset the app so that all components are set to their default values, you should develop a callback function that does that instead of closing and reopening the app.
That being said, here's how you can close the app and open a new instance if that's what you need to do. This callback function makes the currently running instance invisible, opens a second instance with default values, and then deletes the first one.
% Button pushed function: Button
function ButtonPushed(app, event)
% Make current instance of app invisible
app.UIFigure.Visible = 'off';
% Open 2nd instance of app
myFakeApp(); % <--------------The name of your app
% Delete old instance
close(app.UIFigure) %Thanks to Guillaume for suggesting to use close() rather than delete()
end
Note this creates a whole new set of handles to the newly opened app so if any external code interacts with the app and requires access to its handles, those handles will have changed.
댓글 수: 8
AT
2019년 7월 1일
Adam Danz
2019년 7월 1일
The "app" structure contains the handle to every component in your GUI (or at least it should if your GUI was designed properly). The "app" structure should be passed to your "Reset" function. In that function you can merely set the value/propery of each component to whatever default values you choose. You can clear axes, close external figures, clean up the workspace, or whatever else needs done.
You mentioned "I could not come up with any solutions to set all components"; what specifically are you having problems with?
AT
2019년 7월 1일
Adam Danz
2019년 7월 1일
The command clear() clears all variables in the caller workspace. If the command is called from the command window in the base workspace, it will clear all variables in that workspace. If it is called from within a callback function, it will clear all variables from within the function.
To test that, insert the following 3 lines of code in any callback function within the app. It will produce an error.
function ButtonPushed(app, event)
t = 0;
clear(); % <- this will clear the variable 't'
disp(t) % <- this will cause an error b/c t doesn't exist.
end
Guillaume
2019년 7월 1일
minor quibble: App is not a structure but a class object.
Note that you may want to use
close(app.UIFigure);
instead of
delete(app.UIFigure);
in Adam's code, if you want the CloseRequestFcn to be executed when the orignal app is destroyed.
Important: note that the new App is a completely new instance of the app, completely unrelated to the original app. If the app was launched by some code and that code had a handle to the app, then as soon as you reset the app, that handle would no longer be valid. So uiwait and co. won't work with that design.
In my opinion, this is not a good design, particularly considering how slow matlab is at creating app UIFigures. You would be better off putting all your UI initialisation code in the StatupFcn and calling that every time you want to reset the app. This would avoid needlessly destroying a bunch of control to then recreate them.
AT
2019년 7월 1일
Hope Walker
2020년 2월 12일
Hey AT, I was struggling with this same issue for quite a while but I figured out a super easy way that seems to work really well (I haven't had any issues with it so far). All I did was set my startup callback function to how I wanted the app to be when I started and then called that function when a button was pressed.
% Button pushed function: NewQuizButton
function NewQuizButtonPushed(app, event)
startupFcn(app)
end
This seems to avoid having to reset all variables or close and reopen the app while still doing the same thing. Hopefully this helps!
댓글 수: 2
Guillaume
2020년 2월 12일
This is exactly what I suggested to do in my comment above: "You would be better off putting all your UI initialisation code in the StatupFcn and calling that every time you want to reset the app. This would avoid needlessly destroying a bunch of control to then recreate them."
Indeed, this is a much better approach.
I agree this is a more efficient approach than closing/re-opening the app but it also requires more responsibility and attention to detail. The big difference between the two approaches is that in the StartupFcn approach, you must very carefully make sure all components of the app are addressed in the StartupFcn including clearing/resetting the custom property values declared within the app. If a new component is added to the app and you forget to add this component's default value in the startupFcn, it would potentially go unnoticed. Another example would be the use of setappdata where data are stored in the app but not cleared upon reset.
The close-and-reopen method avoids these problems. So, there's a tradeoff to consider.
Xiaohao Guo
2023년 8월 24일
I found it works by creating a temp object defined as follows:
classdef tempObjectForRestarting < handle
properties
app (1,1) appMain
end
methods
function obj = tempObjectForRestarting(app)
obj.app = app;
end
function closeMainApp(obj)
clf;
close all;
delete(obj.app);
obj.app = appMain();
delete(obj);
end
end
end
And in the main app, i use following two lines for restarting:
tempObject = tempObjectForRestarting(app);
closeMainApp(tempObject)
댓글 수: 1
Xiaohao Guo
2023년 8월 24일
where 'appMain()' is the constructor of the main app
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!