3
获取 Matlab 函数的路径
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 函数的路径
第一步,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
题外话,fullfile
是fileparts
的反面:
full_path = fullfile(file_path, [file_name, file_type])
Q. E. D.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK