How do I reset MATLAB to its launched state?

759 views (last 30 days)
Is it possible to "reset" MATLAB? Sometimes I want to "restore" MATLAB to the way it is when it is launched (not the way it was when it is installed). Closing all the figures, resetting the path and clearing variables is relatively easy, but not as easy as:
close all; clear all; path(pathdef); clc;
You need extra flags since the "all" flag is not really all (e.g., a figure might be hidden or mfiles files might be locked).
Resetting non-persistent "default" values is harder. For example, if at some point the "DefaultFigurePosition" is modified:
set(0, 'DefaultFigurePosition', [100, 100, 100, 100])
that change needs to be identified and reversed.
The list of things to get a full reset seems long to me. There are also issues about do I want to restore MATLAB to the way it was when it started or the way it will be the next time it starts. At this point I would be happy with either (or a mix).

Accepted Answer

Jan
Jan on 9 Feb 2011
If you want it clean and simple:
!matlab &
exit
  5 Comments

Sign in to comment.

More Answers (3)

Jan
Jan on 9 Feb 2011
The hidden figures are closed by "close all hidden". Clearing the locked M-files can be controlled by unlocking all files loaded into memory. The default properties are set to the factory values at first and then adjusted to their local values in MATLABRC.
Script file TotalReset.m (not as function!)
% Clear command window:
clc;
% Close figures:
try
close('all', 'hidden');
catch % do nothing
end
% Closing figures might fail if the CloseRequestFcn of a figure
% blocks the execution. Fallback:
AllFig = allchild(0);
if ~isempty(AllFig)
set(AllFig, 'CloseRequestFcn', '', 'DeleteFcn', '');
delete(AllFig);
end
% EDITED: Initialize the default rand stream:
s = RandStream('mt19937ar', 'seed', 0);
RandStream.setDefaultStream(s);
% Clear loaded functions:
% (I avoid "clear all" here for educational reasons)
clear('functions');
clear('classes');
clear('java');
clear('global');
clear('import'); % Not from inside a function!
clear('variables');
% Stop and delete timers:
AllTimer = timerfindall;
if ~isempty(AllTimer) % EDITED: added check
stop(AllTimer);
delete(AllTimer);
end
% Unlock M-files:
LoadedM = inmem;
for iLoadedM = 1:length(LoadedM)
% EDITED: Use STRTOK to consider OO methods:
aLoadedM = strtok(LoadedM{iLoadedM}, '.');
munlock(aLoadedM);
clear(aLoadedM);
end
% Close open files:
fclose('all');
% Reset the warning status EDITED:
warning('on', 'all');
lasterror('reset');
lastwarn('');
% Remove <Default>Properties from root:
prop = get(0, 'default');
propname = fieldnames(prop);
for iprop = 1:length(propname)
set(0, propname{iprop}, 'remove');
end
% EDITED: Change to 1st user path to find "startup.m":
cd(strtok(userpath, pathsep));
% Restore original <Default>Properties of root,
% load default PATH, run STARTUP.m:
matlabrc;
% EDITED: No PACK even in scripts
Limitation: I don't know how to unlock Mex files externally and start a memory cleaning by PACK inside a script or function
EDITED: I've moved my second approach to a new answer.
  15 Comments
Nikolaus Koopmann
Nikolaus Koopmann on 12 Jul 2022
lol.. i was being serious. We're honestly considering moving to another programming language after 4 years of trying with Matlab, mainly because Matlab's garbage collection of object handles is so ineffective and opaque that the software we've built gets bogged down the longer we use it. We have implemented delete() methods for all our classes that break up any references to other objects, but somehow Matlab's performance gets worse and worse the more we use our program.. We always have to kill the Matlab process at some point and load everything anew. The plan is to run our code on a production server eventually, where we won't be able to just restart Matlab via the frontend. Matlab was the choice initally for legacy reasons, but I am 100% sure that this client will not start another project with Matlab again. I would certainly not recommend doing anything OOP with ML after this.
Jan
Jan on 12 Jul 2022
I've misinterpreted the term "parallel universe" :-)
I made some OOP tests with tiny projects and decided to stay at functions and C-Mex. I maintain a program with > 300'000 lines of code for clinical decision making, which runs during the examination. The runtime is limited to 5 seconds to avoid stressing the patients and the staff. This was fine in Matlab 6.5 and R2009a, but opening new figures in HG2 since R2014b slows this down by seconds even on computers, which are 15 years younger. A lot of tricks support the speed limits again, e.g. creating the figures outside the visible area of the screen in advance and filling the individual curves only.
OOP would kick us. But of course OOP techniques are used like encapsulation. Instead of using the nice mechanisms provided by Matlab we use self written tools to control, which functions have read and write acccess to certain fields of structs. Meta-programming - brrr, but it works reliably and fast.
I agree that Matlab's OOP is too slow for some applications. The same argument was used against loops in Matlab 20 years ago. Currently I'm rewriting some simple core functions from vectorized code to loops to save time.

Sign in to comment.


Andreas Goser
Andreas Goser on 9 Feb 2011
Please see if MATLABRC is what you need MATLABRC doc
If it is not completely would you need, you can create a startup.m file and add commands that you need. MATLABRC calls STARTUP.
  1 Comment
Daniel Shub
Daniel Shub on 9 Feb 2011
Yes, using matlabrc is a good way of restoring default things that have been cleared. It does not provide a clearing mechanism and is therefore not a full reset. Jan's answer attempts to clear everything and then use matlabrc to restore. Seems like a reasonable way to go.

Sign in to comment.


Ganesh Tarone
Ganesh Tarone on 10 Apr 2020
Error using connector.internal.doEnsureServiceOn
Error in Connector: Did not start the server. Desired port was: 31515. Last error was: Error while
starting socket: Net Exception: Address already in use: 127.0.0.1:31714, 10048
Error in connector.ensureServiceOn
Warning: Error in Connector: Did not start the server. Desired port was: 31515. Last error was: Error
while starting socket: Net Exception: Address already in use: 127.0.0.1:31714, 10048
> In sltemplate.ui.StartPage/showWithFallback
In sltemplate.ui.StartPage.show
Warning: Failed to start the web server. This could be because of the Java virtual machine. Restart
MATLAB without the flag -NoJVM to fix this.
> In slCustomizer/refresh
In slCustomizer.staticRefresh
In simulinkrc (line 41)
In start_simulink (line 19)
In slLibraryBrowser
In sltemplate.ui.StartPage/showWithFallback
In sltemplate.ui.StartPage.show
Warning: Failed to start the web server. This could be because of the Java virtual machine. Restart
MATLAB without the flag -NoJVM to fix this.
> In slprivate (line 11)
In dig.resetModel (line 7)
In slCustomizer/refresh
In slCustomizer.staticRefresh
In simulinkrc (line 41)
In start_simulink (line 19)
In slLibraryBrowser
In sltemplate.ui.StartPage/showWithFallback
In sltemplate.ui.StartPage.show
Warning: Issue starting Connector. Please check that port 31415 is available. Did not start the server.
Desired port was: 31515. Last error was: Error while starting socket: Net Exception: Address already in
use: 127.0.0.1:31714, 10048
Warning: An error occurred trying to load the Simulink HMI engine. Possible reasons include pressing
Ctrl+C or running MATLAB with the -nojvm option. Close and re-open any open any Simulink models to assure
proper functioning. For proper functioning of Simulink HMI and the Simulation Data Inspector, the JVM is
required.
> In Simulink.HMI.slhmi
In Simulink.HMI.slhmi
I am getting this error after writing simulink in command window ..please help
  1 Comment
Antony Smith
Antony Smith on 30 Aug 2022
The class RandStream has no Constant property or Static method named
'setDefaultStream'.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!