如何在 Matlab 的 save/saveas 中拥有 $HOME?
How to Have $HOME in Matlab's save/saveas?
我已经将我的计算分布到许多平台上,所以我不能使用完整路径,而是像 $HOME
这样的相对路径。
代码
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
也不行。
你怎么能在 Matlab 的 save/saveas 中有 $HOME?
尝试完整路径而不是使用 $HOME。 $HOME是系统命令,在Matlab中无法识别。
使用 \home\YOUR_USER_NAME 而不是 $HOME。
提示:如果您必须在 Matlab 中使用系统命令,system() 是一个有用的函数。
MATLAB 不会自动解析环境变量(例如$HOME
)。一般来说,如果需要环境变量的值,可以使用getenv
.
homedir = getenv('HOME');
或者,在 *nix 上,您 可以 实际上只需使用波浪号 (~
) 来表示文件路径中的用户主目录。
folder = '~/Images';
但是,我通常只依赖 Java 为我获取用户主目录,因为它可以在任何平台上正常工作。
homedir = char(java.lang.System.getProperty('user.home'));
然后,使用fullfile
将您想要的路径连接到用户的主目录。
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
.
我已经将我的计算分布到许多平台上,所以我不能使用完整路径,而是像 $HOME
这样的相对路径。
代码
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
也不行。
你怎么能在 Matlab 的 save/saveas 中有 $HOME?
尝试完整路径而不是使用 $HOME。 $HOME是系统命令,在Matlab中无法识别。
使用 \home\YOUR_USER_NAME 而不是 $HOME。
提示:如果您必须在 Matlab 中使用系统命令,system() 是一个有用的函数。
MATLAB 不会自动解析环境变量(例如$HOME
)。一般来说,如果需要环境变量的值,可以使用getenv
.
homedir = getenv('HOME');
或者,在 *nix 上,您 可以 实际上只需使用波浪号 (~
) 来表示文件路径中的用户主目录。
folder = '~/Images';
但是,我通常只依赖 Java 为我获取用户主目录,因为它可以在任何平台上正常工作。
homedir = char(java.lang.System.getProperty('user.home'));
然后,使用fullfile
将您想要的路径连接到用户的主目录。
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 theHOMEPATH
variable rather thanHOME
.