我遇到过这样的情况:Matlab 程序输出的大量数据需要存储在特定位置,该位置可能因机器而异。我想要的是在每台机器启动时设置的全局变量。然后,各个程序可以使用该变量来确定存储输出的目录。 注意该方法需要平台无关,因为可能有 3 个操作系统将运行这些脚本。
我考虑的一种方法是使用setenv
:
setenv('DATDIR',fullfile(path,to,dir));
然后脚本/函数可以使用以下方式查询该变量:
savedir = getenv('DATDIR');
在这种情况下,变量不受内部clear
的影响Matlab环境。
我想使用的另一种方法是简单地创建一个函数(如matlabroot
),该函数位于路径上并简单地返回分配了数据目录路径的字符串。
function path = datroot()
path = fullfile(path,to,dir);
end
最后,我的问题:还有其他方法可以达到相同的结果吗?如果 Matlab 有自己的环境变量,我宁愿使用它们而不是函数或系统级变量。这可能吗?
I have a situation in which a large amount of data output from Matlab programs needs to be stored in a specific location that can vary from machine to machine. What I'd like to have is a global variable that is set at startup for each machine. This variable could then be used by the various programs in order to determine the directory to store the output. Note the approach will need to be platform independent as there a potentially 3 operating systems that will run these scripts.
One approach I considered is using setenv
:
setenv('DATDIR',fullfile(path,to,dir));
Then the script/function could query this variable using:
savedir = getenv('DATDIR');
In this case the variable is unaffected by clear
inside the Matlab environment.
Another approach I thought to use is simply creating a function (like matlabroot
), which is on the path and simply returns a string to which the data directory path is assigned.
function path = datroot()
path = fullfile(path,to,dir);
end
Finally, to my question: Is there another way to achieve the same result? If Matlab has it's own environment variables, I'd rather use those instead of functions or system-level variables. Is this possible?
发布评论
评论(1)
使用 getpref/setpref/ispref/addpref/< href="http://www.mathworks.com/help/techdoc/ref/rmpref.html" rel="nofollow">rmpref 函数集。根据文档:
这类似于
.ini
或 Java.properties
文件,但使用 MAT 文件来保存值(可以是任何 MATLAB 类型)。在我的 Windows 机器上,使用的文件存储为:Use the getpref/setpref/ispref/addpref/rmpref set of functions. According to the documentation:
This is like
.ini
or Java.properties
files, but instead uses MAT-files to persist the values (which can be of any MATLAB type). On my Windows machine, the file used is stored as: