MATLAB 修改启动路径

MATLAB修改启动有几种方法,其中包括临时的方法和永久性的方法,在这里简要介绍。

cd(change directory)

使用 cd命令,cd是dos系统下的改变路径的命令,全称是 change directory,使用cd命令可以直接的改变当前的工作路径,MATLAB的官方帮助文档说明如下:

cd

Change current foldercollapse all in page

Syntax

cd(newFolder)
oldFolder = cd(newFolder)
cd

Description

cd(newFolder) changes the current folder to newFolder.

oldFolder = cd(newFolder) returns the existing current folder as a string to oldFolder, and then changes the current folder to newFolder.

cd displays the current folder.

Input Arguments

newFolder
A string specifying the folder to which you want to change the current folder. Valid values can be any one of the following:
A full or relative path.
../, which indicates one level up from the current folder.
Multiple strings of ../, which indicates multiple levels up from the current folder.
./, which indicates a path relative to the current folder, although without the ./, cd assumes that the path is relative to the current folder.

Output Arguments

oldFolder
A string specifying the current folder that was in place when you issued the cd command.

Definitions

The current folder is a reference location that MATLAB® uses to find files. This folder is sometimes referred to as the current directory, current working folder, or present working directory.

Examples

Use cd with the matlabroot function to change the current folder to the examples directory for the currently running version of MATLAB:

cd(fullfile(matlabroot, ‘/help/techdoc/matlab_env/examples’))
On a Microsoft® Windows® platform, specify the full path to change the current folder from any location to the examples directory for MATLAB Version 7.11 (R2010b), assuming that version is installed on your C: drive:

cd('C:/Program Files/MATLAB/R2010b/help/techdoc/matlab_env/examples')

% Change the current folder from
% C:/Program Files/MATLAB/R2010b/help/techdoc/matlab_env/examples to
% C:/Program Files/MATLAB/R2010b/help/techdoc:

cd ../..

% Use a relative path to change the current folder from
% C:/Program Files/MATLAB/R2010b/help/techdoc back to
% C:/Program Files/MATLAB/R2010b/help/techdoc/matlab_env/examples:

cd matlab_env/examples

% Change the current folder from its current location to a new
% location,but save its previous location. Later, change the
% current folder to the previous location.

% This returns
% C:/Program Files/MATLAB/R2010b/help/techdoc/matlab_env/examples
% to oldFolder, and then changes the current folder to
% C:/Program Files:

oldFolder = cd('C:/Program Files') 

% Display current folder:

pwd

% Change the current folder to the previous location:
cd(oldFolder)

userpath

userpath

View or change user portion of search pathcollapse all in page

Syntax

userpath example
userpath(newpath) example
userpath(‘reset’)
userpath(‘clear’) example

Description
userpath returns a string specifying the first folder on the search path. MATLAB® adds the userpath to the search path upon startup.

userpath(newpath) sets the primary userpath folder to newpath. The newpath folder appears at the top of the search path immediately and at startup in future sessions. MATLAB removes the folder previously specified by userpath from the search path.
userpath(‘reset’) sets the primary userpath folder to the default for that platform, creating the Documents/MATLAB (or My Documents/MATLAB) folder, if it does not exist. MATLAB immediately adds the default folder to the top of the search path, and also adds it to the search path at startup in future sessions. On Linux, the MATLAB directory is not created if the Documents directory does not exist.

userpath(‘clear’) clears the value for the primary userpath immediately, and for future MATLAB sessions. MATLAB removes the folder previously specified by userpath from the search path.

Examples

  • View userpath Folder

This example assumes userpath is set to the default value on the Windows® XP platform, My Documents\MATLAB. Start MATLAB and display the current folder:

pwd
H:\My Documents\MATLAB

In this example, H is the drive at which My Documents is located.

Confirm that the current folder is the userpath.

userpath
H:\My Documents\MATLAB;

Display the search path.

path

MATLABPATH

H:\My Documents\MATLAB
C:\Program Files\MATLAB\R2009a\toolbox\matlab\general
C:\Program Files\MATLAB\R2009a\toolbox\matlab\ops


MATLAB returns the search path. The userpath portion is at the top.

  • Set New Value for userpath

Assume userpath is set to the default value on the Windows XP platform, My Documents\MATLAB. Change the value from the default for userpath to C:\Research_Project.

newpath = 'C:\Research_Project';
userpath(newpath)

View the effect of the change on the search path.

path

MATLABPATH

C:\Research_Project
C:\Program Files\MATLAB\R2009a\toolbox\matlab\general
C:\Program Files\MATLAB\R2009a\toolbox\matlab\ops


MATLAB displays the search path, with the new value for userpath portion at the top. Note that MATLAB automatically removed the previous value of userpath, H:\My Documents\MATLAB, from the search path when you assigned a new value to userpath.

  • Clear the Value for userpath

Assume userpath is set to the default value and you do not want any folders to be added to the search path upon startup. Confirm the default is currently set.

userpath
H:\My Documents\MATLAB;

Verify that the userpath folder is at the top of the search path.

path

MATLABPATH

H:\My Documents\MATLAB
C:\Program Files\MATLAB\R2009a\toolbox\matlab\general
C:\Program Files\MATLAB\R2009a\toolbox\matlab\ops
...

Clear the value.

userpath('clear')

Verify the result.

userpath

ans =

Confirm the userpath folder was removed from the search path.

path

MATLABPATH

C:\Program Files\MATLAB\R2009a\toolbox\matlab\general
C:\Program Files\MATLAB\R2009a\toolbox\matlab\ops

Note: If you clear the value for userpath, the startup folder will not necessarily be on the search path. Removing the userpath folder from the search path and saving the changes to the path has the same effect.

Input Arguments

collapse all
newpath — New userpath value
string
New userpath value, specified as a string. newpath must be an absolute path.

Example: 'C:\myFolder'

savepath

savepath
Save current search pathcollapse all in page

Syntax
savepath
savepath folderName/pathdef.m example
status = savepath(_)

Description
savepath saves the current MATLAB® search path to an existing pathdef.m file in the current folder. If there is no pathdef.m file in the current folder, then savepath saves the search path to the first pathdef.m file on the current path. If there is no such file on the current path, then savepath saves the search path to the pathdef.m file that MATLAB located at startup.

On a Windows® system with User Account Control (UAC) enabled, you might be prompted to allow the update operation because it requires administrator-level permission.

savepath folderName/pathdef.m saves the current search path to pathdef.m located in the folder specified by folderName. If you do not specify folderName, then savepath saves pathdef.m in the current folder.

Use this syntax if you do not have write access to the current pathdef.m file.

status = savepath(_) additionally indicates if the operation is successful, using any of the input arguments in the previous syntaxes. The status output is 0 when savepath is successful, and 1 otherwise.

Examples

  • Save Search Path to Specific Folder

Save the current search path to pathdef.m located in the folder, I:/my_matlab_files.

savepath I:/my_matlab_files/pathdef.m

Input Arguments

folderName — Folder name
string
Folder name, specified as a string. folderName can be a relative or absolute path.

Example: C:\myFolder
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值