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!