3

获取 Matlab 函数的路径

 3 years ago
source link: https://zhiqiang.org/coding/get-matlab-function-directory.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

获取 Matlab 函数的路径

作者: 张志强

, 发表于 2014-04-01

, 共 565 字 , 共阅读 310 次

第一步,which函数可用来获取 Matlab 函数的全路径(包含路径和文件名)。

>> a = which('startup')
a =
D:\Program Files\MATLAB\toolbox\local\startup.m

若在函数内部要获取自己的全路径,也可以使用mfilename函数:

a = mfilename('fullpath');
% a = 'D:\Program Files\MATLAB\toolbox\local\startup'

注意 mfilename 返回的值不带文件类型后缀。

第二步,使用fileparts函数将全路径分解为路径、文件名、文件后缀三部分:

>> [file_path, file_name, file_type] = fileparts(a)
file_path =
D:\Program Files\MATLAB\toolbox\local
file_name =
startup
file_type =
.m

题外话,fullfilefileparts的反面:

full_path = fullfile(file_path, [file_name, file_type])

Q. E. D.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK