How to Have $HOME in Matlab's save/saveas?

狂风中的少年 提交于 2019-12-23 02:17:33

问题


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'));

Output

Error using save
Cannot create '777.mat' because '$HOME/Images' does not exist.

Error in masiCool (line 98)
    save(strcat(filename,'.mat'),'masi');

Backward flash \$HOME does not work neither.


How can you have $HOME in Matlab's save/saveas?


回答1:


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');

Alternately, on *nix, you can actually just use the tilde (~) to represent the user's home directory in a filepath.

folder = '~/Images';

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'));

Then, use fullfile to concatenate the path you want onto the user's home directory.

filename = fullfile(homedir, 'Images', sprintf('%d.mat', item))

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 the HOMEPATH variable rather than HOME.




回答2:


Try full path instead of using $HOME. $HOME is a system command and it is not recognizable in Matlab.

Use \home\YOUR_USER_NAME instead of $HOME.

TIP: In case you have to use a system command inside Matlab, system() is a useful function.



来源:https://stackoverflow.com/questions/35887777/how-to-have-home-in-matlabs-save-saveas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!