MATLAB Answers

How do I reset MATLAB to its launched state?

1,488 views (last 30 days)
Daniel Shub
Daniel Shub on 9 Feb 2011
Answered: Ganesh Tarone on 10 Apr 2020
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).

  0 Comments

Sign in to comment.

Accepted Answer

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

  2 Comments

Daniel Shub
Daniel Shub on 9 Feb 2011
The more I/we play, the more I think this is the correct answer.
Jan
Jan on 15 Feb 2011
I'm very proud of finding out, that EXIT sets Matlab to a well defined status.

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.

  11 Comments

Show 8 older comments
K E
K E on 2 Dec 2015
I would have never found the following in the documentation, but it's very useful so thanks Jan:
set(0, propname{iprop}, 'remove');
Jan
Jan on 5 Apr 2017
The tic timer is not cleared yet. This does not show an error:
tic; TotalReset; toc

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

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products