Clear Filters
Clear Filters

restart an app with code

117 views (last 30 days)
AT
AT on 30 Jun 2019
Commented: Xiaohao Guo on 24 Aug 2023
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 Comment
Guillaume
Guillaume on 30 Jun 2019
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.

Sign in to comment.

Answers (3)

Adam Danz
Adam Danz on 1 Jul 2019
Edited: Adam Danz on 1 Jul 2019
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 Comments
AT
AT on 1 Jul 2019
Thank you, Adam and Guillaume. I will try to write the alternative you suggested.
Adam Danz
Adam Danz on 2 Jul 2019
Edited: Adam Danz on 15 Jul 2019
Glad I could help. Let us know if you get stuck.

Sign in to comment.


Hope Walker
Hope Walker on 12 Feb 2020
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 Comments
Guillaume
Guillaume on 12 Feb 2020
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.
Adam Danz
Adam Danz on 12 Feb 2020
Edited: Adam Danz on 12 Feb 2020
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.

Sign in to comment.


Xiaohao Guo
Xiaohao Guo on 24 Aug 2023
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 Comment
Xiaohao Guo
Xiaohao Guo on 24 Aug 2023
where 'appMain()' is the constructor of the main app

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!