Why does MATLAB crash when I use a WRITE or PRINT statement in my FORTRAN MEX-file?

4 views (last 30 days)
Here is an example from my program:
#include <fintrf.h>
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
C-----------------------------------------------------------------------
C (integer) Replace integer by integer*8 on the DEC Alpha and the
C SGI 64-bit platforms
C
MWPOINTER PLHS(*), PRHS(*)
C-----------------------------------------------------------------------
C
INTEGER NLHS, NRHS
C
C WRITE(6,*) 'hello'
PRINT *, 'hello'
C
RETURN
END

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Jan 2010
To resolve this issue, use MEXPRINTF command instead of WRITE or PRINT.
Here is a modified example that uses MEXPRINTF:
#include <fintrf.h>
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
C-----------------------------------------------------------------------
C (integer) Replace integer by integer*8 on the DEC Alpha and the
C SGI 64-bit platforms
C
MWPOINTER PLHS(*), PRHS(*)
C-----------------------------------------------------------------------
C
INTEGER NLHS, NRHS
C
C WRITE(6,*) 'hello'
CALL MEXPRINTF("hello")
C PRINT *, 'hello'
C
RETURN
END
NOTE: The MEXPRINTF function is set up to only accept a string as an input argument. It does not accept a variable as an argument. As a possible workaround, try using the mexCallMatlab or the mexEvalString function with the DISP function to display the contents of the variable. This would entail putting the variable to be displayed in the MATLAB workspace first.
For example, to print the value of NUM at the MATLAB prompt with MLEVALSTRING:
#include <fintrf.h>
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
C-----------------------------------------------------------------------
C (integer) Replace integer by integer*8 on the DEC Alpha and the
C SGI 64-bit platforms
C
MWPOINTER PLHS(*), PRHS(*)
C-----------------------------------------------------------------------
C
INTEGER NLHS, NRHS
INTEGER NUM
REAL*8 VALUE
C
C-----------------------------------------------------------------------
C (integer) Replace integer by integer*8 on the DEC Alpha and the
C SGI 64-bit platforms
C
C-----------------------------------------------------------------------
NUM = MXCREATEDOUBLEMATRIX(1,1,0)
VALUE = 10
CALL MXCOPYREAL8TOPTR(VALUE, MXGETPR(NUM), 1)
CALL MEXPUTVARIABLE('base', 'num', NUM)
CALL MEXEVALSTRING("sprintf('NUM is %i', num)")
END
Many users have told us that they cannot print newline characters using the above workarounds; there is no direct way to have newlines printed from within a FORTRAN MEX-file. However, as a workaround, you can use the MEXCALLMATLAB routine to run an MATLAB file that prints a newline character to the screen:
CALL MEXCALLMATLAB(0, NULL, 0, NULL, "new_line")
Here's what new_line.m would look like:
function new_line
fprintf(1, '\n');
This may not be a very elegant solution, but is the only workaround to the problem.
For versions prior to MATLAB 7.2 (R2006a) please see related solution 1-16LLH

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2006a

Community Treasure Hunt

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

Start Hunting!