How can I read an environment variable in Fortran?

1 次查看(过去 30 天)
I'm trying to read an environment variable set by Matlab's setenv() function inside a Fortran .mex. It sounds simple enough... My mex function in Fortran looks essentially like:
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
INTEGER NLHS, NRHS
mwPointer PLHS(*), PRHS(*)
mwSize maxbuff
PARAMETER(maxbuff = 512)
CHARACTER(maxbuff) input_buf, output_buf
C Get the string contents (dereference the input pointer).
status = mxGetString(prhs(1), input_buf, maxbuf)
IF (status .NE. 0) THEN
call mexErrMsgIdAndTxt ('MATLAB:revord:readError',
+ 'Error reading string.')
ENDIF
C Initialize outbuf_buf to blanks. This is necessary on some compilers.
output_buf = ' '
C Call the computational subroutine.
CALL GETENV(input_buf,output_buf)
C Set output_buf to MATLAB mexFunction output.
plhs(1) = mxCreateString(output_buf)
IF (plhs(1) .EQ. 0) THEN
CALL mexErrMsgIdAndTxt ('FORTRAN:getenvmf:pushevalue',
+ 'Error pushing evalue to Matlab.')
ENDIF
C
RETURN
END
I compile and run it as:
>> mex getenvf.f -f mexopts64.bat
>>getenvf('OS')
ans =
Windows_NT <-- The pre-existing DOS environment is fine.
>> setenv('Foo','bar')
>> getenv('Foo')
ans =
bar <-- Matlab's getenv() works fine.
>> getenvf('Foo')
ans =
'' <-- The MATLAB-added environment variable is not loaded to the .mex's workspace.
Neither 32 nor 64 bit R2013b works. Any ideas as to where I went wrong?
getenvf.zip includes the following files:
getenvf.f <-- Fortran file containing a mexFunction that calls Fortran's getenv() subroutine and returns the value.
mexopts32.bat <--Option 9 modified for Intel Visual Fortran 13.0 SP1 (with Microsoft Visual C++ 2012 linker)
mexopts64.bat <--Option 9 modified for Intel Visual Fortran 13.0 SP1 (with Microsoft Visual C++ 2012 linker).
getenvf.mexw32 <-- my resulting .mexw32.
getenvf.mexw64 <-- my resulting .mexw64.
  1 个评论
Bob Clark
Bob Clark 2016-10-12
New information: Compiling in R2013b generates a mexw32 that works in all versions of Matlab before R2013b. Compiling my sample code in R2011b generates a getenvf.mexw32 that works in all versions. Its something to do with R2013b.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by