在Matlab中:如何读取相对于要读取的函数的数据文件,而函数的调用方位于其他地方?所以我有以下星座:
X:callScript.m
Y:myFunction.m
Y:datadataFile.txt
callScript.m
addpath('Y:');
myFunction();
myFunction.m
function myFunction()
fid = fopen('./data/dataFile.txt'); % < does not work!
% ... read something ...
fclose(fid);
end
我认为这是一个更好的方法,但这也能达到的效果
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,'datadatafile.txt']);
% ... read something ...
fclose(fid);
end