Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Beginner
84 Views

question about calling matlab function

hello:

        I'm learning about how to call matlab function by fortran , and I start with the example  matlabroot/extern/ examples/eng_mat/fengdemo.F, here is the code:

 

#include "engine.h"        
#include "fintrf.h"
#if 0
C     fengdemo.F
C     .F file need to be preprocessed to generate .for equivalent
C
#endif
C
C     fengdemo.f
C     This is a simple program that illustrates how to call the MATLAB
C     Engine functions from a FORTRAN program.
C
C Copyright 1984-2005 The MathWorks, Inc.
C======================================================================
C $Revision: 1.9.4.4 $
      program main
C-----------------------------------------------------------------------
C     (pointer) Replace integer by integer*8 on 64-bit platforms
C
      mwpointer engOpen, engGetVariable, mxCreateDoubleMatrix
      mwpointer mxGetPr
      mwpointer ep, T, D 
C----------------------------------------------------------------------
C
C     Other variable declarations here
      double precision time(10), dist(10)
      integer engPutVariable, engEvalString, engClose
      integer temp, status
      data time / 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 /
      ep = engOpen('matlab ')
      if (ep .eq. 0) then
         write(6,*) 'Can''t start MATLAB engine'
         stop
      endif
      T = mxCreateDoubleMatrix(1, 10, 0)
      call mxCopyReal8ToPtr(time, mxGetPr(T), 10)
C     Place the variable T into the MATLAB workspace
      status = engPutVariable(ep, 'T', T)
      if (status .ne. 0) then 
         write(6,*) 'engPutVariable failed'
         stop
      endif
C     Evaluate a function of time, distance = (1/2)g.*t.^2
C     (g is the acceleration due to gravity)
      if (engEvalString(ep, 'D = .5.*(-9.8).*T.^2;') .ne. 0) then
         write(6,*) 'engEvalString failed'
         stop
      endif
C     Plot the result
      if (engEvalString(ep, 'plot(T,D);') .ne. 0) then 
       write(6,*) 'engEvalString failed'
         stop
      endif
      if (engEvalString(ep, 'title(''Position vs. Time'')') .ne. 0) then
          write(6,*) 'engEvalString failed'
          stop
      endif
      if (engEvalString(ep, 'xlabel(''Time (seconds)'')') .ne. 0) then
          write(6,*) 'engEvalString failed'
          stop
      endif
      if (engEvalString(ep, 'ylabel(''Position (meters)'')') .ne. 0)then
          write(6,*) 'engEvalString failed'
          stop
      endif
C     read from console to make sure that we pause long enough to be
C     able to see the plot
      print *, 'Type 0 <return> to Exit'
      print *, 'Type 1 <return> to continue'
      read(*,*) temp
      if (temp.eq.0) then
         print *, 'EXIT!'
         status = engClose(ep)
         if (status .ne. 0) then 
            write(6,*) 'engClose failed'
         endif
         stop
      end if
      if (engEvalString(ep, 'close;') .ne. 0) then
         write(6,*) 'engEvalString failed'
         stop
      endif 
      D = engGetVariable(ep, 'D')
      call mxCopyPtrToReal8(mxGetPr(D), dist, 10)
      print *, 'MATLAB computed the following distances:'
      print *, '  time(s)  distance(m)'
      do 10 i=1,10
         print 20, time(i), dist(i)
 20      format(' ', G10.3, G10.3)
 10   continue
      call mxDestroyArray(T)
      call mxDestroyArray(D)
      status = engClose(ep)
      if (status .ne. 0) then 
         write(6,*) 'engClose failed'
         stop
      endif
      stop
      end

I use  ifort -I '/opt/MATLAB/extern/include' -B '/opt/MATLAB/extern/lib/glnx86'  fengdemo.F  to compile this,but it shows:

 

tmwtypes.h(125): #error: #if: syntax error.
tmwtypes.h(137): #error: #if: syntax error.
tmwtypes.h(149): #error: #if: syntax error.
tmwtypes.h(161): #error: #if: syntax error.
tmwtypes.h(173): #error: #if: syntax error.
tmwtypes.h(703): #error: #if: syntax error.
tmwtypes.h(776): #warning: keyword redefined: false
tmwtypes.h(779): #warning: keyword redefined: true
tmwtypes.h(790): #error: #if: syntax error.
tmwtypes.h(791): #error:  "This code must be compiled using a 2's complement representation for signed integer values"
features.h(184): #error: #if: syntax error.
features.h(202): #error: #if: syntax error.
features.h(206): #error: #if: syntax error.
features.h(210): #error: #if: syntax error.
features.h(214): #error: #if: syntax error.
features.h(218): #error: #if: syntax error.
features.h(322): #error: #if: syntax error.
cdefs.h(145): #error: #if: syntax error.
cdefs.h(304): #error: #if: syntax error.
stdlib.h(530): #error: incomplete macro call __nonnull.
stdlib.h(532): #error: '#ifdef' argument starts with wrong symbol.
stdlib.h(606): #error: '#ifdef' argument starts with wrong symbol.
stdlib.h(611): #error: #if: syntax error.
features.h(20): #error: '#ifndef' argument starts with wrong symbol.
features.h(21): #error: bad macro name.
features.h(118): #error: bad macro name.
features.h(122): #error: bad macro name.
features.h(184): #error: #if: syntax error.
features.h(202): #error: #if: syntax error.
features.h(206): #error: #if: syntax error.
features.h(210): #error: #if: syntax error.
features.h(214): #error: #if: syntax error.
features.h(218): #error: #if: syntax error.
features.h(322): #error: #if: syntax error.
assert.h(109): #error: #if: syntax error.

I don't know why ,could you help me ?

0 Kudos
20 Replies
Beginner
8 Views

 

Hi 

Just came across this thread when I was searching for clues. I got this working in OS X 10.10 with latest Matlab 2014b

I was getting the following when compiling naively from the command line

Undefined symbols for architecture x86_64:
  "_mxcopyptrtoreal8730_", referenced from:
      _MAIN__ in fengdemo.o
  "_mxcopyreal8toptr730_", referenced from:
      _MAIN__ in fengdemo.o
  "_mxcreatedoublematrix730_", referenced from:
      _MAIN__ in fengdemo.o
  "_mxdestroyarray_", referenced from:
      _MAIN__ in fengdemo.o
  "_mxgetpr_", referenced from:
      _MAIN__ in fengdemo.o
ld: symbol(s) not found for architecture x86_64

After some fiddling I got
 ./fengdemo
dyld: Library not loaded: @rpath/libeng.dylib
  Referenced from: /Users/ericnoodle/dummydesktop/build/./fengdemo
  Reason: image not found
Trace/BPT trap: 5

after more fiddling I got 
matlab: Command not found.
 Can't start MATLAB engine

 

so in order to get it to work I did the followingf

from bash, $MLDIR is my Matlab install folder in /Applications

DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$MLDIR/bin/maci64/

and to fix the $PATH
PATH=$PATH:$MLDIR/bin

then compile, link and run
ifort -I $MLDIR/extern/include/ -c -fPIC fengdemo.F90
ifort  fengdemo.o -o fengdemo -L $MLDIR/bin/maci64/ -leng -lmat -lmx
./fengdemo

There may be a more efficient way ? but it works

- hope it helps and it'll be useful for me in a few months time when I forgot what I did ;-)

 

XXXXXXXXXXXXX:~/dummydesktop/build$ ./fengdemo

 Type 0 <return> to Exit

 Type 1 <return> to continue

1

 MATLAB computed the following distances:

   time(s)  distance(m)

   1.00     -4.90    

   2.00     -19.6    

   3.00     -44.1    

   4.00     -78.4    

   5.00     -123.    

   6.00     -176.    

   7.00     -240.    

   8.00     -314.    

   9.00     -397.    

   10.0     -490.    

XXXXXXXXX:~/dummydesktop/build$

 

0 Kudos