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?
Use the getpref/setpref/ispref/addpref/rmpref set of functions. According to the documentation:
Preference values are persistent and maintain their values between MATLAB sessions. Where they are stored is system dependent.
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:
%APPDATA%\MathWorks\MATLAB\R2010b\matlabprefs.mat
精彩评论