Does setenv not set variables for MEX functions?
Show older comments
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
Accepted Answer
More Answers (1)
Kaustubha Govind
on 9 Aug 2012
0 votes
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 Comments
Walter Roberson
on 10 Aug 2012
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
on 10 Aug 2012
Do you think that there might be a difference on the Windows OS version? Perhaps Dan was not using Windows NT?
Walter Roberson
on 10 Aug 2012
"Windows NT" is what shows up as the OS for anything derived from Windows NT, including XP, Vista, and Windows 7.
Kaustubha Govind
on 10 Aug 2012
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
on 10 Aug 2012
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!