如何用m脚本把Set Path 设置为默认值

6 次查看(过去 30 天)
Vico Wu
Vico Wu 2022-8-3
我想通过脚本将上面个的路径恢复成默认状态,手动操作中还会提示save path 需要设定为No
上面的操作如何用 m脚本实现.

回答(2 个)

Nithin Kumar
Nithin Kumar 2023-9-8
Hi Vico,
I understand that you want to restore the provided path to the default state using m-script.
To restore a provided path to the default state in MATLAB using an m-script, use the “restoredefaultpath” function. It removes all directories from the MATLAB search path and restores it to the default state.
To set the provided path to its default state in MATLAB, kindly refer to the following steps:
1. Create a new `.m` script file, e.g., "restore_default_path.m".
2. Open the script file in the MATLAB editor.
3. Add the following code to the script:
restoredefaultpath;
4. Save the script file.
5. To run the script and restore the MATLAB path to its default value, execute it by typing its name (without the `.m` extension) in the MATLAB Command Window.
After running the above script, all directories previously added to the MATLAB search path will be removed, and the path will be restored to its default state.
For more information regarding "restoredefaultpath" function, kindly refer to the following documentation:
I hope this provides you with the required information regarding your query.
Regards,
Nithin Kumar.

Paras Gupta
Paras Gupta 2023-9-9
Hi,
I understand that you want to restore the MATLAB search path to its default state and disable the prompt for saving the path using a MATLAB script.
Please find the code below to achieve the same.
% Clear the current search path
restoredefaultpath;
% Disable the prompt for saving the path
prefFile = fullfile(prefdir, 'matlab.prf'); % Get the path to the MATLAB preferences file
prefs = fileread(prefFile); % Read the contents of the preferences file
prefs = strrep(prefs, 'EditorPromptOnClearPath=0', 'EditorPromptOnClearPath=1'); % Replace the line that sets EditorPromptOnClearPath with 0
fid = fopen(prefFile, 'w'); % Open the preferences file for writing
fwrite(fid, prefs); % Write the modified preferences
fclose(fid); % Close the file
% Save the current search path
savepath;
You can refer to the following documentations for more information on the code:
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 环境和设置 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!