在Matlab中:如何读取相对于想要读取它的函数的数据文件,而函数的调用者位于其他地方?所以我有以下星座:
X:\callScript.m
Y:\myFunction.m
Y:\data\dataFile.txt
callScript.m
addpath('Y:\');
myFunction();
myFunction.m
function myFunction()
fid = fopen('./data/dataFile.txt'); % < does not work!
% ... read something ...
fclose(fid);
end
答案 0 :(得分:1)
我认为这是一种更好的方法,但这也可以解决问题
function myFunc()
path_myFunc = which('myFunc'); % get myFunc's full path
path_myFunc = path_myFunc(1:find(path_myFunc,10,'last'); % Find last '\'(=10)
fid = fopen([path_myFunc,'\data\datafile.txt']);
% ... read something ...
fclose(fid);
end