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.
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.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!