在Matlab中获取“文档"路径 [英] get 'Documents' path in Matlab
问题描述
我了解您在大多数情况下可能可以使用以下代码来完成工作:
I understand you can probably use the following code to get the job done in most cases:
mydocpath = fullfile(getenv('USERPROFILE'), 'Documents');
但是,如果用户已将"Doucments"文件夹移动到其他位置,例如:E:\Documents
,则上述代码将不起作用,因为getenv('USERPROFILE')
始终返回C:\Users\MY_USER_NAME
.
However, if the user has moved 'Doucments' folder to a different location, for example: E:\Documents
, the above code won't work, since getenv('USERPROFILE')
always returns C:\Users\MY_USER_NAME
.
在C#中,可以使用Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
,它总是返回正确的路径,而不管文档"在哪里. Matlab中有类似的东西吗?
In C#, one can use Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
, which always returns the correct path regardless where 'Documents' is. Is there anything similar in Matlab?
我当前的解决方案相当笨拙,可能不安全:
My current solution is rather clumsy and probably unsafe:
% search in the MATLAB path lists
% this method assumes that there is always a path containing \Documents\MATLAB registered already
searchPtn = '\Documents\MATLAB';
pathList = strsplit(path,';');
strIdx = strfind(pathList, searchPtn);
candidateIdx = strIdx{find(cellfun(@isempty,strIdx)==0, 1)}(1);
myDocPath = pathList{candidateIdx}(1 : strIdx{candidateIdx}+ numel(searchPtn));
推荐答案
基于@excaza的建议,我使用此处以查询注册表.
Based on @excaza 's suggestion, I came up with a solution using dos
and the cmd command found here to query the registry.
% query the registry
[~,res]=dos('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal');
% parse result
res = strsplit(res, ' ');
myDocPath = strtrim(res{numel(res)});
如果尚未将客户PC中的文档文件夹重新定位或移动到环境路径之一(例如%SYSTEMROOT%),则上述方法将返回
If the document folder in customer's PC has not been relocated or moved to one of the environment path such as %SYSTEMROOT%, the above method would return
%SOME_ENVIRONMENT_PATH%/Documents(Or a custom folder name)
以上路径在Matlab的mkdir之类的功能中不起作用或不存在,它们将以%SOME_ENVIRONMENT_PATH%
作为文件夹名称.因此,我们需要检查返回值中是否存在环境路径,并获取正确的路径:
The above path will not work in Matlab's functions such as mkdir or exist, which will take %SOME_ENVIRONMENT_PATH%
as a folder name. Therefore we need to check for the existence of environment path in the return value and get the correct path:
[startidx, endidx] = regexp(myDocPath,'%[A-Z]+%');
if ~isempty(startidx)
myDocPath = fullfile(getenv(myDocPath(startidx(1)+1:endidx(1)-1)), myDocPath(endidx(1)+1:end));
end
完整代码:
% query the registry
[~,res]=dos('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal');
% parse result
res = strsplit(res, ' ');
% get path
myDocPath = strtrim(res{numel(res)});
% if it returns %AAAAA%/xxxx, meaning the Documents folder is
% in some environment path.
[startidx, endidx] = regexp(myDocPath,'%[A-Z]+%');
if ~isempty(startidx)
myDocPath = fullfile(getenv(myDocPath(startidx(1)+1:endidx(1)-1)), myDocPath(endidx(1)+1:end));
end
这篇关于在Matlab中获取“文档"路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!