Does setenv not set variables for MEX functions?
显示 更早的评论
Hello,
I have noticed that when I use setenv in MATLAB on Windows, the change is not visible in MEX functions. The behavior is as I would expect on Linux. I tested the following MEX function on MATLAB 2011a and 2012a:
#include <mex.h>
void mexFunction(int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin[])
{
printf("OS %s\n", getenv("OS"));
printf("AAA %s\n", getenv("AAA"));
}
When I run test from MATLAB comandline I get
>> test
OS Windows_NT
AAA (null)
I get exactly the same when I setenv('AAA') to some value:
>> setenv('AAA','BBB')
>> getenv('AAA')
ans =
BBB
>> test
OS Windows_NT
AAA (null)
Is that a feature, or is it a bug?
Thanks a lot!
Marcin
采纳的回答
更多回答(1 个)
Kaustubha Govind
2012-8-9
0 个投票
I think this is an OS-defined behavior and not specific to MATLAB. Extrapolating from this discussion, the loader (which is what calls into the MEX-file - since MEX-files are essentially DLLs/shared libraries) reads the environment variables at the time of MATLAB startup, and does not notice your changes to the environment variables, which in turn means that the MEX-file doesn't see them.
6 个评论
Walter Roberson
2012-8-10
I believe that discussion only applies to the LD_LIBRARY_PATH, as a security measure in the OS being discussed (OS-X).
The setenv() behavior that Marcin is seeing on Linux is what I would expect on any POSIX-compliant system. (Hmmm -- I really ought to buy an electronic copy of the POSIX.1 standards!)
Windows XP SP2 and later are supposedly POSIX compliant (but I have my personal doubts about the completeness of that compliance.)
Kaustubha Govind
2012-8-10
Do you think that there might be a difference on the Windows OS version? Perhaps Dan was not using Windows NT?
Walter Roberson
2012-8-10
"Windows NT" is what shows up as the OS for anything derived from Windows NT, including XP, Vista, and Windows 7.
Kaustubha Govind
2012-8-10
Ah! Okay then. I'm still inclined to think that this might be OS-defined behavior, but I don't have much experience with this, so I would recommend contacting MathWorks Tech Support at this point. Sorry!
angainor
2012-8-10
类别
在 帮助中心 和 File Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!