Gateway mexFunction - Matrix equal to zero
Ältere Kommentare anzeigen
Goodmorning. I have some problem on writing and using mexFunction. I have a fortran code where I read some input file. I write the mexFunction for this code and so I can read all this file. The fortran code is right and It can read the files and in matlab I can call the fortran function (by building the mexw32 file). My problem is that the matlab matrix generated by this function have the values all equal to zero. If you have any suggestion...thanks a lot. Stefano
Here It is my code:
#include <fintrf.h>
SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
C-----------------------------------------------------------------------
C (pointer) Replace integer by integer*8 on 64-bit platforms
C
MWPOINTER PLHS(*), PRHS(*)
C-----------------------------------------------------------------------
C
INTEGER NLHS, NRHS
C
C-----------------------------------------------------------------------
C (pointer) Replace integer by integer*8 on 64-bit platforms
C
MWPOINTER MXCREATEDOUBLEMATRIX, MXGETPR
C-----------------------------------------------------------------------
C
MWSIZE MXGETM, MXGETN
C
C KEEP THE ABOVE SUBROUTINE, ARGUMENT, AND FUNCTION DECLARATIONS FOR USE
C IN ALL YOUR FORTRAN MEX FILES.
C---------------------------------------------------------------------
C
C-----------------------------------------------------------------------
C (pointer) Replace integer by integer*8 on 64-bit platforms
C
MWPOINTER ncolture1P, VECthetaFCP, VECthetaWPP, VECREWP,
$ phiP, zetaP, frazP, nrsbP
C-----------------------------------------------------------------------
C
MWSIZE M, N, NEL
REAL*8 Rncolture1P, RVECthetaFCP(625), RVECthetaWPP(625),
$ RVECREWP(625), RphiP(625), RzetaP(625), RfrazP(625,8),
$ RnrsbP
#if defined MSWIND
C For Windows only!
C This resets the floating point exception to allow divide by zero,
C overflow and invalid numbers.
C
INTEGER(2) CONTROL
CALL GETCONTROLFPQQ(CONTROL)
CONTROL = CONTROL .OR. FPCW$ZERODIVIDE
CONTROL = CONTROL .OR. FPCW$INVALID
CONTROL = CONTROL .OR. FPCW$OVERFLOW
CALL SETCONTROLFPQQ(CONTROL)
#endif
C
C CHECK FOR PROPER NUMBER OF ARGUMENTS
C
IF (NRHS .NE. 1) THEN
CALL MEXERRMSGTXT('YPRIME requires one input arguments')
ELSEIF (NLHS .GT. 7) THEN
CALL MEXERRMSGTXT('YPRIME requires seven output argument')
ENDIF
C
C CREATE A MATRIX FOR RETURN ARGUMENT
C
PLHS(1) = MXCREATEDOUBLEMATRIX(1,1,0)
PLHS(2) = MXCREATEDOUBLEMATRIX(625,1,0)
PLHS(3) = MXCREATEDOUBLEMATRIX(625,1,0)
PLHS(4) = MXCREATEDOUBLEMATRIX(625,1,0)
PLHS(5) = MXCREATEDOUBLEMATRIX(625,1,0)
PLHS(6) = MXCREATEDOUBLEMATRIX(625,1,0)
PLHS(7) = MXCREATEDOUBLEMATRIX(625,8,0)
C
C ASSIGN POINTERS TO THE VARIOUS PARAMETERS
C
ncolture1P = MXGETPR(PLHS(1)) ! LEFT HAND -> PARAMETRI DI OUTPUT
VECthetaFCP = MXGETPR(PLHS(2))
VECthetaWPP = MXGETPR(PLHS(3))
VECREWP = MXGETPR(PLHS(4))
phiP = MXGETPR(PLHS(5))
zetaP = MXGETPR(PLHS(6))
frazP = MXGETPR(PLHS(7))
nrsbP = MXGETPR(PRHS(1)) ! RIGHT HAND -> PARAMETRO DI INPUT
C
C COPY RIGHT HAND ARGUMENTS TO LOCAL ARRAYS OR VARIABLES
NEL = 1
CALL MXCOPYPTRTOREAL8(nrsbP, RnrsbP, NEL)
C
C DO THE ACTUAL COMPUTATIONS IN A SUBROUTINE
C CREATED ARRAYS.
C
CALL leggiETparam(Rncolture1P, RVECthetaFCP, RVECthetaWPP,
$ RVECREWP, RphiP, RzetaP, RfrazP, RnrsbP)
C
C COPY OUTPUT WHICH IS STORED IN LOCAL ARRAY TO MATRIX OUTPUT
NEL = 1
CALL MXCOPYREAL8TOPTR(Rncolture1P, ncolture1P, NEL)
NEL = 625
CALL MXCOPYREAL8TOPTR(RVECthetaFCP, VECthetaFCP, NEL)
NEL = 625
CALL MXCOPYREAL8TOPTR(RVECthetaWPP, VECthetaWPP, NEL)
NEL = 625
CALL MXCOPYREAL8TOPTR(RVECREWP, VECREWP, NEL)
NEL = 625
CALL MXCOPYREAL8TOPTR(RphiP, phiP, NEL)
NEL = 625
CALL MXCOPYREAL8TOPTR(RzetaP, zetaP, NEL)
NEL = 625
CALL MXCOPYREAL8TOPTR(RfrazP, frazP, NEL)
C
RETURN
END
5 Kommentare
Stefano Basso
am 14 Feb. 2011
Laurens Bakker
am 27 Feb. 2012
Hi Stefano,
Could you please use the 'code' button at the top of the edit screen to re-format your code as actual code? That will help us understand your question.
Cheers,
Laurens
James Tursa
am 27 Feb. 2012
How do you know the file is being read properly? Could you put in some mexPrintf statements right after the leggiETparam call and print out the results directly to make sure they are not all zero?
James Tursa
am 27 Feb. 2012
P.S. Why isn't your last NEL = 625 * 8 for RfrazP?
webdesign
am 26 Mär. 2024
Hi Stefano,
It looks like you're encountering an issue where the matrices generated by your Fortran function in MATLAB are all zeros. Here are a few suggestions that might help you diagnose and solve the problem:
- Check Data Transfer: Make sure that the data is being correctly transferred from your Fortran code to MATLAB. Specifically, check the calls to MXCOPYREAL8TOPTR and the number of elements (NEL) being transferred.
- Variable Initialization: Ensure all variables in your Fortran code are properly initialized before attempting to pass them to MATLAB. Uninitialized variables could lead to undefined behavior.
- Error Handling: Check if there are error handling mechanisms in your Fortran code that might catch any errors that could occur. It's possible that an error is occurring before the data is successfully passed to MATLAB, but without proper error handling, this might go unnoticed.
If nothing helps, feel free to write me again :).
Antworten (0)
Kategorien
Mehr zu Fortran Source MEX Files finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!