如何在Matlab的保存/保存中包含$ HOME? [英] How to Have $HOME in Matlab's save/saveas?
问题描述
我已经将计算分配到许多平台,所以我不能使用全路径,而只能使用相对路径,例如$HOME
.
代码
I have distributed my computing to many platforms so I cannot use fullpath but instead relative like $HOME
.
Code
filename=strcat('$HOME/Images/',num2str(item);
save(strcat(filename,'.mat'),'masi');
saveas(her, strcat(filename,'.png'));
输出
Error using save
Cannot create '777.mat' because '$HOME/Images' does not exist.
Error in masiCool (line 98)
save(strcat(filename,'.mat'),'masi');
向后闪光\$HOME
也不起作用.
Backward flash \$HOME
does not work neither.
如何在Matlab的保存/保存中包含$ HOME?
How can you have $HOME in Matlab's save/saveas?
推荐答案
环境变量(例如$HOME
)不会被MATLAB自动解析.通常,如果需要环境变量的值,则可以使用 getenv
Environment variables (such as $HOME
) are not automatically parsed by MATLAB. In general, if you need the value of an environment variable, you can use getenv
.
homedir = getenv('HOME');
或者,在* nix上,您可以实际上仅使用波浪号(~
)来表示文件路径中用户的主目录.
Alternately, on *nix, you can actually just use the tilde (~
) to represent the user's home directory in a filepath.
folder = '~/Images';
但是,我通常仅依靠 Java 为我获取用户主目录,因为它可以在任何平台上正常工作.
However, I typically just rely upon the Java to get the user home directory for me since it will work properly on any platform.
homedir = char(java.lang.System.getProperty('user.home'));
然后,使用 fullfile
进行连接您想要进入用户主目录的路径.
Then, use fullfile
to concatenate the path you want onto the user's home directory.
filename = fullfile(homedir, 'Images', sprintf('%d.mat', item))
注意:如果要在不使用JVM的HPC或MATLAB的某些实例上执行此操作.
getenv
选项是最好的.在Windows上,您希望使用HOMEPATH
变量而不是HOME
.
NOTE: If you are wanting to do this on an HPC or some instance of MATLAB that is not using the JVM. The
getenv
option is best. On Windows, you would want theHOMEPATH
variable rather thanHOME
.
这篇关于如何在Matlab的保存/保存中包含$ HOME?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!