问题
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
getenvoption is best. On Windows, you would want theHOMEPATHvariable rather thanHOME.
回答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