Re: pass 1D vector from matlab to fortran
- From: "Eric Jäger" <eric.jaeger.nospam@xxxxxxxxxxxxx>
- Date: Mon, 31 Dec 2007 13:24:55 +0000 (UTC)
James Tursa <aclassyguywithaknotac@xxxxxxxxxxx> wrote in
message <4t9hn3h8t88pq1pkff9i0e4b06vfub8ito@xxxxxxx>...
On Mon, 31 Dec 2007 07:16:03 +0000 (UTC), "Eric Jäger"able to just
<eric.jaeger.nospam@xxxxxxxxxxxxx> wrote:
I only have an example code for passing a 2D matrix from
matlab to fortran (and then again back to matlab):
plhs(1)=mxCreateNumericArray(2,[10;100],mxClassIDFromClassName('double'),0)
output1=mxGetPr(plhs(1))
%Call Fortran code
call_stl(%val(input1),%val(input2),%val(output1))
How do I have to change the code to pass an 1D vector (lets
say size: 1*1000) from matlab to fortran?
If you have working code for a 2D matrix, you should be
replace the dimension values 10 & 100 with your new values1 & 1000
and it should work just fine, even if the Fortran variableis only one
dimension. A 1D variable in MATLAB is treated as 2D withone of the
dimensions 1 (a scalar is a 2D with both of the dimensions1). You
don't show much code for your example so I can't give youspecific
advice. What are input1 and input2? Are they the resultof another
mxGetPr call somewhere in your code? Is your call_stl(...)working for
2D? etc. etc.
James Tursa
Hi,
Thanks for your help. The fortran routine does only work
with 1D timeseries. Therefore, I put the 1D output in a 2D
matrix first, then I return it to matlab. Everything seems
to work fine, except for the return of the output from
matlab to fortran.
Below you will find the matlab code and parts of the (pretty
long) fortran code...
%input: timeseries ts and constants
%output: timeseries trend and season
[ trend season ] = stl_new(ts,int32(45),int32(12));
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
integer*4 plhs(*), prhs(*)
integer*4 nlhs, nrhs
integer*4 tdiff4,period4
integer*4 stl_y,stl_trend,stl_seasonal
integer*4 dims(2)
c Externals
integer*4 mxGetPr
integer*4 mxCreateNumericArray
integer*4 mxClassIDFromClassName
c Check for proper number of arguments.
if (nrhs .ne. 3) then
call mexErrMsgTxt('Three inputs required')
else if(nlhs .ne. 2) then
call mexErrMsgTxt('Two outputs required')
endif
c Check that all inputs are numeric
if ((mxIsNumeric(prhs(1)).eq.0).or.
> (mxIsNumeric(prhs(2)).eq.0).or.
> (mxIsNumeric(prhs(3)).eq.0)) then
call mexErrMsgTxt('Input must be numeric.')
endif
c Get the tdiff and period from pointer array to Fortran
array
call mxCopyPtrToInteger4(mxGetPr(prhs(2)),tdiff4,1)
call mxCopyPtrToInteger4(mxGetPr(prhs(3)),period4,1)
c Get pointer for input of y timeseries vector
stl_y=mxGetPr(prhs(1))
c Allocate memory for output and set pointer to output!
Matlab vectors are also 2D
dims(2)=tdiff4*period4 !length of timeseries
dims(1)=1
plhs(1)=mxCreateNumericArray(2,dims,
> mxClassIDFromClassName('double'),0)
plhs(2)=mxCreateNumericArray(2,dims,
> mxClassIDFromClassName('double'),0)
stl_trend=mxGetPr(plhs(1))
stl_seasonal=mxGetPr(plhs(2))
c Call to computational routine:
call
call_stl(%val(stl_y),%val(stl_trend),%val(stl_season),
> tdiff4,period4)
return
end
c Interface routine to STL FORTRAN routine
subroutine call_stl(m_y,m_trend,m_season,m_tdiff,m_period)
implicit none
c
------------------------------------------------------------------
c Declaration of parameters and variables
c
------------------------------------------------------------------
c Input data table
integer m_tdiff
integer m_period
real*8 m_y(m_tdiff*m_period)
c STL decomposition
integer n
integer np
integer ns,nt,nl
integer isdeg,itdeg,ildeg
integer nsjump,ntjump,nljump
integer ni,no
c Output data table
real*8 mseason(m_tdiff*m_period)
real*8 mtrend(m_tdiff*m_period)
real*8 m_season(1,m_tdiff*m_period)
real*8 m_trend(1,m_tdiff*m_period)
c Auxiliary variables
integer i
c
------------------------------------------------------------------
c Main
c
------------------------------------------------------------------
n=m_tdiff*m_period ! Number of values in y
np=m_period ! Period of the seasonal
component
ns=209 !69 !35 ! Length of the seasonal
smoother
nt=119 !39 !19 ! Length of the trend smoother
nl=13 ! Length of the low-pass
filter.
isdeg=0 ! Degree of locally-fitted
polynomial in seasonal smoothing
itdeg=0 ! Degree of locally-fitted
polynomial in trend smoothing
ildeg=0 ! Degree of locally-fitted
polynomial in low-pass smoothing
nsjump=1 ! Skipping value for
seasonal smoothing
ntjump=1 ! Skipping value for trend
smoothing
nljump=1 ! Skipping value for
low-pass smoothing
ni=1 ! Number of loops for
updating the seasonal and trend components
no=5 ! Number of iterations of
robust fitting (3)
call
stl(m_y,n,np,ns,nt,nl,isdeg,itdeg,ildeg,nsjump,ntjump,
> nljump,ni,no,mseason,mtrend)
do i=1,n
m_season(1,i)=mseason(i)
m_trend(1,i)=mtrend(i)
enddo
return
end
c
------------------------------------------------------------------
c STL decomposition
c Decompose time series into trend + seasonal
c Cleveland and Cleveland
c Taken from www.netlib.org
c
------------------------------------------------------------------
subroutine stl(y,n,np,ns,nt,nl, isdeg,itdeg,ildeg,
& nsjump,ntjump,nljump, ni,no, season,trend)
implicit none
integer n, np, ns,nt,nl, isdeg,itdeg,ildeg,
nsjump,ntjump,nljump,
& ni, no
double precision y(n), rw(n), season(n), trend(n),
& work(n+2*np,5)
.....
.
- References:
- pass 1D vector from matlab to fortran
- From: Eric Jäger
- Re: pass 1D vector from matlab to fortran
- From: James Tursa
- pass 1D vector from matlab to fortran
- Prev by Date: surface construction with matlab
- Next by Date: passing data with ode solver
- Previous by thread: Re: pass 1D vector from matlab to fortran
- Next by thread: Alternative to Mathlab for arbitrary precision
- Index(es):
Relevant Pages
|