How will running the "restoredefaultpath" and "savepath" commands affect my custom paths and how do I back up my custom paths?

136 views (last 30 days)
I setup custom paths from within MATLAB but now need to restore the default path. How does the "restoredefaultpath" command affect my custom paths and how can I back them up before running it?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 21 Jul 2021
Edited: MathWorks Support Team on 21 Jul 2021
MATLAB will read its path from a file called pathdef.m. There may be multiple pathdef.m files on your computer but MATLAB will only read one at a time. MATLAB will read the pathdef.m file in your MATLAB startup folder in your Documents folder first. To determine which pathdef.m file is currently being used by MATLAB, run the command:
which -all pathdef
Before proceeding, you should backup this file by copying it to a folder outside of your MATLAB path.
The which -all command will list all pathdef.m files that MATLAB can find. The first one on this list is the one MATLAB is currently using.
If you run the "restoredefaultpath" command, MATLAB will remove the current path from this session and will then search through the MATLAB installation directory and add all toolbox folders back to the path for this session. It will only hold this in memory and not save it for future sessions. To make this change permanent, run the command:
The savepath command will save this session's MATLAB path to the pathdef.m that is listed first in the output of which -all pathdef. It will overwrite the existing contents of this file.
How to Back Up Custom Paths
You can back up your active pathdef.m file by moving to a folder that is not on your MATLAB path or by renaming it. 
If you have multiple release of MATLAB installed, using a pathdef.m file in your MATLAB startup directory, in your Documents folder, can be problematic. All installed versions will read this pathdef.m by default. This pathdef.m file will be specific to the version of MATLAB in which you last saved the path. In other versions of MATLAB, the paths may not be accurate and this may cause problems starting MATLAB or errors in the MATLAB command window at startup. 
To avoid this issue you can instead create a startup.m file that will add your custom paths to MATLAB's path at the beginning of each session without changing the contents of the pathdef.m file. This will allow MATLAB to maintain user-specific custom paths without altering MATLAB's default path for that release. For more details on this, please refer to the below documentation for "addpath":
  1 Comment
Razvan Ciocan
Razvan Ciocan on 10 Mar 2020
Thank you ! You are right. In this morning I run first the example VisualizeFeatures workbook and worked
Then I tried to run script bellow.
It is nothing a combination of 3 of your examples. I was able to reproduce the error message
% japanese classify
% Load the Japanese Vowels training data. XTrain is a cell array containing 270 sequences
%of dimension 12 of varying length. Y is a categorical vector of labels "1","2",...,"9",
%which correspond to the nine speakers. The entries in XTrain are matrices with 12 rows
%(one row for each feature) and varying number of columns (one column for each time step).
[XTrain,YTrain] = japaneseVowelsTrainData;
% Visualize the first time series in a plot. Each line corresponds to a feature.
figure (1)
xlabel("Time Step")
title("Training Observation 1")
legend("Feature " + string(1:12),'Location','northeastoutside')
figure (2)
xlabel("Time Step")
title("Training Observation 2")
legend("Feature " + string(1:12),'Location','northeastoutside')
% Get the sequence lengths for each observation.
numObservations = numel(XTrain);
for i=1:numObservations
sequence = XTrain{i};
sequenceLengths(i) = size(sequence,2);
%Sort the data by sequence length.
[sequenceLengths,idx] = sort(sequenceLengths);
XTrain = XTrain(idx);
YTrain = YTrain(idx);
% View the sorted sequence lengths in a bar chart.
ylim([0 30])
title("Sorted Data")
% Choose a mini-batch size of 27 to divide the training data evenly and reduce the amount of
%padding in the mini-batches. The following figure illustrates the padding added to the sequences.
miniBatchSize = 27;
% Define the LSTM network architecture. Specify the input size to be sequences of size 12
%(the dimension of the input data). Specify an bidirectional LSTM layer with 100 hidden units,
%and output the last element of the sequence. Finally, specify nine classes by including a fully connected
%layer of size 9, followed by a softmax layer and a classification layer.
% If you have access to full sequences at prediction time, then you can use a bidirectional LSTM layer
%in your network. A bidirectional LSTM layer learns from the full sequence at each time step.
%If you do not have access to the full sequence at prediction time, for example,
%if you are forecasting values or predicting one time step at a time, then use an LSTM layer instead.
inputSize = 12;
numHiddenUnits = 100;
numClasses = 9;
layers = [ ...
maxEpochs = 100;
miniBatchSize = 27;
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'GradientThreshold',1, ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'SequenceLength','longest', ...
'Shuffle','never', ...
'Verbose',0, ...
%Now, specify the training options. Specify the solver to be 'adam', the gradient threshold to be 1,
%and the maximum number of epochs to be 100. To reduce the amount of padding in the mini-batches,
%choose a mini-batch size of 27. To pad the data to have the same length as the longest sequences,
%specify the sequence length to be 'longest'. To ensure that the data remains sorted by sequence length,
%specify to never shuffle the data.
%Since the mini-batches are small with short sequences, training is better suited for the CPU.
%Specify 'ExecutionEnvironment' to be 'cpu'. To train on a GPU, if available, set 'ExecutionEnvironment'
%to 'auto' (this is the default value).
maxEpochs = 100;
miniBatchSize = 27;
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'GradientThreshold',1, ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'SequenceLength','longest', ...
'Shuffle','never', ...
'Verbose',1, ...
% train net
net = trainNetwork(XTrain,YTrain,layers,options);
%Load the test set and classify the sequences
%XTest is a cell array containing 370 sequences of dimension 12 of varying length.
%YTest is a categorical vector of labels "1","2",..."9",
[XTest,YTest] = japaneseVowelsTestData;
%The LSTM network net was trained using mini-batches of sequences of similar length.
%Ensure that the test data is organized in the same way. Sort the test data by sequence length.
numObservationsTest = numel(XTest);
for i=1:numObservationsTest
sequence = XTest{i};
sequenceLengthsTest(i) = size(sequence,2);
[sequenceLengthsTest,idx] = sort(sequenceLengthsTest);
XTest = XTest(idx);
YTest = YTest(idx);
%Classify the test data. To reduce the amount of padding introduced by the classification process,
%set the mini-batch size to 27.
%To apply the same padding as the training data, specify the sequence length to be 'longest'.
miniBatchSize = 27;
YPred = classify(net,XTest, ...
'MiniBatchSize',miniBatchSize, ...
acc = sum(YPred == YTest)./numel(YTest)
%figure (2)
plotconfusion(YTest,YPred,'Training Accuracy')
hold off
%For each time step of the sequences, get the activations output
%by the LSTM layer (layer 2) for that time step and update the network state.
X = XTest{1};
sequenceLength = size(X,2);
idxLayer = 2;
outputSize = net.Layers(idxLayer).NumHiddenUnits;
for i = 1:sequenceLength
features(:,i) = activations(net,X(:,i),idxLayer);
[net, YPred(i)] = classifyAndUpdateState(net,X(:,i));
% Visualize the first 10 hidden units using a heatmap.
xlabel("Time Step")
ylabel("Hidden Unit")
title("LSTM Activations")

Sign in to comment.

More Answers (2)

Patrick MOUREY
Patrick MOUREY on 12 Dec 2017
I have perform your solution. When i try the savepath command i still have an issue This is the output error Warning: C:\Program Files (x86)\MATLAB\R2010bSP2\toolbox\local\pathdef.m not found. Toolbox Path Cache is not being used. Type 'help toolbox_path_cache' for more info Undefined function or variable 'ispc'. Warning: MATLAB did not appear to successfully set the search path. To recover for this session of MATLAB, type "restoredefaultpath;matlabrc". To find out how to avoid this warning the next time you start MATLAB, type "docsearch problem path" after recovering for this session. Warning: Duplicate directory name: C:\Program Files (x86)\MATLAB\R2010bSP2\toolbox\local. Warning: Initializing Handle Graphics failed in matlabrc. This indicates a potentially serious problem in your MATLAB setup, which should be resolved as soon as possible. Error detected was: MATLAB:UndefinedFunction Undefined function or method 'colordef' for input arguments of type 'double'. > In matlabrc at 100 Warning: Initializing Java preferences failed in matlabrc. This indicates a potentially serious problem in your MATLAB setup, which should be resolved as soon as possible. Error detected was: MATLAB:UndefinedFunction Undefined function or method 'usejava' for input arguments of type 'char'. > In matlabrc at 123 Warning: Failed to add default profiler filters. > In matlabrc at 179 >> restoredefaultpath >> which -all pathdef 'pathdef' not found. >> savepath Warning: Unable to save path to file ''. > In savepath at 162
Please tke in mind that it is ablocking issue fior us and please contact us by phone as soon as possible

Patrick MOUREY
Patrick MOUREY on 12 Dec 2017
At this time we have resolved by ourself the pathdef issues Nethertheless we still have a blocking issues. Focus on the installation: - we have lauched the Matlab installer - we have download from your website (after identification using our login and licence) the zip of Simulink We lay the stress on the fact the Simulink is not installed on our PC despite of a full installation Can you help us and give us a straigt way to install the toolbox because we spent too time for this and we can not wait more... Sorry for the urgence but we are awaiting a more straig solution Feel free to contact me by phone in order to find quickly a solution +33 05 34 61 97 81 Regards


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


No tags entered yet.

Community Treasure Hunt

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

Start Hunting!