From: James Tursa on
"Etienne" <etienne.coetzee(a)airbus.com> wrote in message <hkfljo$fpm$1(a)fred.mathworks.com>...
> Thanks James
>
> I will try it tomorrow. We are half a day ahead of you folks. I assume you are in the USA.
>
> Regards
>
> Etienne

Some time ago you asked for an example using my Fortran 95 interface package. Here is how to code up this example using this package. (caveat: untested since I am not on a MATLAB computer at the moment).

James Tursa

#include <fintrf.h>

SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
use MatlabAPImex
use MatlabAPImx
IMPLICIT NONE
!-ARG
MWPOINTER PLHS(*), PRHS(*)
INTEGER*4 NLHS, NRHS
!-LOC
Real*8, pointer :: NDIM
mwPointer ptrNDIM
mwIndex :: i = 1
!-----
!\
! Argument checks
!/
if( nrhs /= 1 ) then
call mexErrMsgTxt("Need exactly one auto input")
endif
if( mxGetClassName(prhs(1)) /= "auto" ) then
call mexErrMsgTxt("Need exactly one auto input")
endif
if( nlhs > 1 ) then
call mexErrMsgTxt("Too many outputs")
endif
!\
! Get the Ndim property
!/
ptrNDIM = mxGetProperty(PLHS(1),i,'Ndim')
!\
! Ndim Property check
!/
if( ptrNDIM == 0 ) then
call mexErrMsgTxt("Property 'Ndim' not found")
endif
NDIM => fpGetPr0(ptrNDIM) ! NDIM points directly at data area of ptrNDIM
if( .not.associated(NDIM) ) then
call mexErrMsgTxt("Property 'Ndim' is not a scalar double")
endif
!\
! Double the NDIM value. Note that NDIM points directly into the
! ptrNDIM mxArray variable. Changing NDIM changes the data area of
! ptrNDIM directly. No need for the mxCopy___ routines.
!/
NDIM = 2.d0 * NDIM
!\
! Create the output array
!/
PLHS(1) = mxDuplicateArray(PRHS(1))
!\
! Set the new Ndim property value on the returned mxArray variable
!/
CALL mxSetProperty(PLHS(1),i,'Ndim',ptrNDIM)
CALL mxDestroyArray(ptrNDIM)
!-----
RETURN
END
From: Etienne on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hkgc5l$9um$1(a)fred.mathworks.com>...
> "Etienne" <etienne.coetzee(a)airbus.com> wrote in message <hkfljo$fpm$1(a)fred.mathworks.com>...
> > Thanks James
> >
> > I will try it tomorrow. We are half a day ahead of you folks. I assume you are in the USA.
> >
> > Regards
> >
> > Etienne
>
> Some time ago you asked for an example using my Fortran 95 interface package. Here is how to code up this example using this package. (caveat: untested since I am not on a MATLAB computer at the moment).
>
> James Tursa
>
> #include <fintrf.h>
>
> SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
> use MatlabAPImex
> use MatlabAPImx
> IMPLICIT NONE
> !-ARG
> MWPOINTER PLHS(*), PRHS(*)
> INTEGER*4 NLHS, NRHS
> !-LOC
> Real*8, pointer :: NDIM
> mwPointer ptrNDIM
> mwIndex :: i = 1
> !-----
> !\
> ! Argument checks
> !/
> if( nrhs /= 1 ) then
> call mexErrMsgTxt("Need exactly one auto input")
> endif
> if( mxGetClassName(prhs(1)) /= "auto" ) then
> call mexErrMsgTxt("Need exactly one auto input")
> endif
> if( nlhs > 1 ) then
> call mexErrMsgTxt("Too many outputs")
> endif
> !\
> ! Get the Ndim property
> !/
> ptrNDIM = mxGetProperty(PLHS(1),i,'Ndim')
> !\
> ! Ndim Property check
> !/
> if( ptrNDIM == 0 ) then
> call mexErrMsgTxt("Property 'Ndim' not found")
> endif
> NDIM => fpGetPr0(ptrNDIM) ! NDIM points directly at data area of ptrNDIM
> if( .not.associated(NDIM) ) then
> call mexErrMsgTxt("Property 'Ndim' is not a scalar double")
> endif
> !\
> ! Double the NDIM value. Note that NDIM points directly into the
> ! ptrNDIM mxArray variable. Changing NDIM changes the data area of
> ! ptrNDIM directly. No need for the mxCopy___ routines.
> !/
> NDIM = 2.d0 * NDIM
> !\
> ! Create the output array
> !/
> PLHS(1) = mxDuplicateArray(PRHS(1))
> !\
> ! Set the new Ndim property value on the returned mxArray variable
> !/
> CALL mxSetProperty(PLHS(1),i,'Ndim',ptrNDIM)
> CALL mxDestroyArray(ptrNDIM)
> !-----
> RETURN
> END

Nice and neat. Thanks. I will use your toolbox with the current code I am using.
From: James Tursa on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hkgc5l$9um$1(a)fred.mathworks.com>...
>
> ptrNDIM = mxGetProperty(PLHS(1),i,'Ndim')

Typo. Should be:

ptrNDIM = mxGetProperty(prhs(1),i,'Ndim')

James Tursa
From: Etienne on
Hi James

I am now trying to assign an array to the Ndim variable, so when it comes in, it is empty, the array is resized to 90 elemnts, and I would like to send this back out as the Ndim variable.

I have tried to reallocate memory to the pointer, but I am not quite sure what I am doing wrong.

Any help would be appreciated.

Regards

Etienne
From: James Tursa on
"Etienne" <etienne.coetzee(a)airbus.com> wrote in message <hleh0q$1sa$1(a)fred.mathworks.com>...
> Hi James
>
> I am now trying to assign an array to the Ndim variable, so when it comes in, it is empty, the array is resized to 90 elemnts, and I would like to send this back out as the Ndim variable.
>
> I have tried to reallocate memory to the pointer, but I am not quite sure what I am doing wrong.
>
> Any help would be appreciated.
>
> Regards
>
> Etienne

Not exactly sure how to advise without seeing your code. If you are simply trying to create a new 90 element Ndim variable to use, then you can just create one using the ptrNDIM variable, point to it with a 1-D NDIM pointer using the fpGetPr1 routine, and then use it. e.g.,

#include <fintrf.h>

SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS)
use MatlabAPImex
use MatlabAPImx
IMPLICIT NONE
!-ARG
MWPOINTER PLHS(*), PRHS(*)
INTEGER*4 NLHS, NRHS
!-LOC
Real*8, pointer :: NDIM(:)
mwPointer ptrNDIM
mwIndex :: i = 1
mwSize :: m = 90
mwSize :: n = 1
!-----
!\
! Argument checks
!/
if( nrhs /= 1 ) then
call mexErrMsgTxt("Need exactly one auto input")
endif
if( mxGetClassName(prhs(1)) /= "auto" ) then
call mexErrMsgTxt("Need exactly one auto input")
endif
if( nlhs > 1 ) then
call mexErrMsgTxt("Too many outputs")
endif
!\
! Create a new Ndim property mxArray, 90 x 1 double
!/
ptrNDIM = mxCreateDoubleMatrix(m, n, mxREAL)
NDIM => fpGetPr1(ptrNDIM) ! NDIM points directly at data area of ptrNDIM
if( .not.associated(NDIM) ) then
call mexErrMsgTxt("Internal error pointing to ptrNDIM data")
endif
!\
! Fill the Ndim property values
!/
NDIM = 5.d0 ! Your actual values would go here
!\
! Create the output array
!/
PLHS(1) = mxDuplicateArray(PRHS(1))
!\
! Set the new Ndim property value on the returned mxArray variable
!/
CALL mxSetProperty(PLHS(1),i,'Ndim',ptrNDIM)
CALL mxDestroyArray(ptrNDIM)
!-----
RETURN
END


James Tursa