Prev: help
Next: Finding equilibrium points of a non-linear, instable, and stiff ODE system of 5 eqs.
From: Etienne on 22 Feb 2010 12:20 Hi Folks I am trying to use the MexCallMatlabWithTrap command from a mex file, and cannot seem to get the error message from the MException object. Or I seem to get the first character and not the rest. The code is below. I have marked the code of interest with "C !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!". It is the CALL mxCopyPtrToCharacter(MSGptr2,MSG,N) That only seems to copy the first character instead of the whole message. Any help would be appreictaed, thank you. Etienne C=============================================== SUBROUTINE FUNC(NDIM,U,ICP,PAR,IJAC,F,DFDU,DFDP) C USE AUTO_CONSTANTS, ONLY:ODIM,OUT,NOUTX,NPARX,NBIFX,NIAP,NRAP C C Arguments C IMPLICIT DOUBLE PRECISION (A-H,O-Z) DIMENSION U(NDIM),PAR(*),F(NDIM) C C Matlab mx and mex functions and pointers C MWPOINTER MSGptr1, MSGptr2, K, MEXCALLMATLABWITHTRAP MWSIZE :: N mwIndex :: i = 1 DOUBLEPTR MXGETPR CHARACTER(4) FSTR CHARACTER(300) MSG INTEGER K INTEGER MXCREATESTRING INTEGER MEXCALLMATLAB INTEGER MXISDOUBLE INTEGER MXISINF INTEGER MXISNAN INTEGER MXGETNUMBEROFELEMENTS INTEGER MXGETNUMBEROFDIMENSIONS INTEGER NUMEL INTEGER NUMPAR INTEGER PR INTEGER TF INTEGER TRAPFLAG LOGICAL OK LOGICAL, EXTERNAL :: NUMSTAB REAL*8 NDM C C C Define the dimensions of states in MATLAB environment C NDM = NDIM PRHS(1) = MXCREATEDOUBLEMATRIX(1,1,MXREAL) CALL MXCOPYREAL8TOPTR(NDM,MXGETPR(PRHS(1)),1) C C Define the state variable in MATLAB environment C PRHS(2) = MXCREATEDOUBLEMATRIX(NDIM,1,MXREAL) CALL MXCOPYREAL8TOPTR(U,MXGETPR(PRHS(2)),NDIM) C C Define the parameter variable in MATLAB environment C PRHS(3) = MXCREATEDOUBLEMATRIX(NPARX,1,MXREAL) CALL MXCOPYREAL8TOPTR(PAR,MXGETPR(PRHS(3)),NPARX) C C Call the m-file AUTOEQN for derivatives C K = MEXCALLMATLABWITHTRAP(2,PLHS,3,PRHS,"FUNC") CALL MXDESTROYARRAY(PRHS(3)) CALL MXDESTROYARRAY(PRHS(2)) CALL MXDESTROYARRAY(PRHS(1)) C C !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! IF( K /= 0 ) THEN MSGptr1=mxGetProperty(K,i,'message') MSGptr2=mxGetPr(MSGptr1) N=300 CALL mxCopyPtrToCharacter(MSGptr2,MSG,N) CALL mxDestroyArray(MSGptr1) CALL mxDestroyArray(MSGptr2) C !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CALL AUTOSTOPWITHERROR(MSG) ENDIF C C Some other code here C C RETURN END
From: James Tursa on 22 Feb 2010 13:09 "Etienne" <etienne.coetzee(a)airbus.com> wrote in message <hlueck$n78$1(a)fred.mathworks.com>... > Hi Folks > > I am trying to use the MexCallMatlabWithTrap command from a mex file, and cannot seem to get the error message from the MException object. Or I seem to get the first character and not the rest. The code is below. I have marked the code of interest with "C !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!". It is the > > CALL mxCopyPtrToCharacter(MSGptr2,MSG,N) > > That only seems to copy the first character instead of the whole message. > > Any help would be appreictaed, thank you. > > Etienne Although not at all evident from the doc (I had to do a lot of experimenting on my own to discover this), the mxCopyPtrToCharacter function is for use with C-style strings (yes I know ... even though this is a Fortran routine it *does* expect a C-style string). That is, the Ptr is expected to be a pointer to a C-style string, which means it is expecting a null terminated string of 1-byte characters. (If you look at the note near the top of my MatlabAPImx.f file you will see this). But MATLAB does not store strings this way ... MATLAB stores strings as 2-bytes per character and the string is not null terminated. In your case, the first MATLAB string character is a 2-byte quantity, one byte being the actual character value (assuming ASCII encoding) and the other byte being 0. So the mxCopyPtrToCharacter routine simply sees this as a string of length 1 ... i.e. it interprets the 2nd 0 byte in the first MATLAB 2-byte character as the end of the C-style string. That is why you are only getting one 1-byte character copied. The solution is to use the correct routine that is intended to be used with MATLAB char arrays, namely mxGetString. James Tursa
From: Etienne on 22 Feb 2010 15:14 "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hluh80$gpp$1(a)fred.mathworks.com>... > "Etienne" <etienne.coetzee(a)airbus.com> wrote in message <hlueck$n78$1(a)fred.mathworks.com>... > > Hi Folks > > > > I am trying to use the MexCallMatlabWithTrap command from a mex file, and cannot seem to get the error message from the MException object. Or I seem to get the first character and not the rest. The code is below. I have marked the code of interest with "C !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!". It is the > > > > CALL mxCopyPtrToCharacter(MSGptr2,MSG,N) > > > > That only seems to copy the first character instead of the whole message. > > > > Any help would be appreictaed, thank you. > > > > Etienne > > Although not at all evident from the doc (I had to do a lot of experimenting on my own to discover this), the mxCopyPtrToCharacter function is for use with C-style strings (yes I know ... even though this is a Fortran routine it *does* expect a C-style string). That is, the Ptr is expected to be a pointer to a C-style string, which means it is expecting a null terminated string of 1-byte characters. (If you look at the note near the top of my MatlabAPImx.f file you will see this). But MATLAB does not store strings this way ... MATLAB stores strings as 2-bytes per character and the string is not null terminated. In your case, the first MATLAB string character is a 2-byte quantity, one byte being the actual character value (assuming ASCII encoding) and the other byte being 0. So the mxCopyPtrToCharacter routine simply sees this as a string of length 1 ... i.e. it interprets the 2nd 0 byte > in the first MATLAB 2-byte character as the end of the C-style string. That is why you are only getting one 1-byte character copied. The solution is to use the correct routine that is intended to be used with MATLAB char arrays, namely mxGetString. > > James Tursa Thanks James I actually tried to think how I can rewrite the above code with your toolbox. This is actually code you helped me with about a year ago, before your new toolbox. I will give it a go. I have to say that you are probably one of the most helpful Newsgroup members. Are there any prizes for that? I would have to nominate you if there is. Regards Etienne
|
Pages: 1 Prev: help Next: Finding equilibrium points of a non-linear, instable, and stiff ODE system of 5 eqs. |