Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29236 Discussions

Setting Requirement for Fortran call Matlab function written by user

guenkeng
Beginner
1,137 Views

Hi,

I have the problem calling Matlab function from the main program written in Fortran. Thereis no problem in compiling and building the program, but during execution, the following error was shown:

forrt1: severe(157): Program Exception -access violation

Image PC Routine Line Source

Stack trace terminated abnormally.

I am using Compaq Visual Fortran 6 and Matlab R13 for my programming. I have tried all the programs provided from Matlab extern example folder they are work perfectly. However, when I wrote my own Matlab function called by the main fortran program, error occurred as mentioned above.

Can anyone help me to solve my problem. Thank you!

0 Kudos
6 Replies
Jugoslav_Dujic
Valued Contributor II
1,137 Views
It's difficult to figure out what is wrong without at least some of the source.

A wild guess of mine is that you tried to pass a literal constant or a PARAMETER to a routine argument which is modifiable by Matlab. But there might be a thousands other causes.
0 Kudos
guenkeng
Beginner
1,137 Views

I have solved that problem as mentioned earlier but encounter another problem. That is I have written the following simple fortran code for testing but the result is not what I expected:

program main

integer engOpen, engGetVariable, mxCreateDoubleMatrix
integer mxGetPr
integer A, B, teststatus, Z

integer engPutVariable, engEvalString, engClose

A = mxCreateDoubleMatrix(1, 1, 0)
call mxCopyReal8ToPtr(2, mxGetPr(A), 1)

teststatus = engPutVariable(ep, 'A', A)

if (teststatus .ne. 0) then
write(6,*) 'engPutVariable A failed'
stop
endif

if (engEvalString(ep, 'B = A*5;') .ne. 0) then
write(6,*) 'engEvalString failed'
stop
endif

B = engGetVariable(ep, 'B')
call mxCopyPtrToReal8(mxGetPr(B), Z, 1)

write (*,*) "Z=", Z

call mxDestroyArray(A)
call mxDestroyArray(B)
status = engClose(ep)

if (status .ne. 0) then
write(6,*) 'engClose failed'
stop
endif

stop
end

The final result is Z which should be equal to 10 but the result is 0. I am not sure where go wrong with the program. Could any one help me?

Furthermore, if I would like to write a maltab function to be call by my fortran main program, what command should I used. I have came accross the command "mexCallMATLAB", but it seem to be used when the fortran is a subrountine of a main Matlab function, which the fortran subrountine named as "mexFunction" will then calla matlab function. However, my main function is in Fortran, not inmatlab. Hence, anyone know what command to use and which directory should the written matlab function saved so that when calling of the funciton will not result in linking error?

0 Kudos
guenkeng
Beginner
1,137 Views

Now, I am able to obtain the multiplication of Z*5=10 when calling Matlab engine from Fortran. However, I couldn't get the desire result when I perform the following:

B=sin(A), B=cos(A), B=A*A, B=A.^2, B=2+A ... and so on as long as the expression is not simply multiplication of integer value. Anyone know the why is this so?

0 Kudos
guenkeng
Beginner
1,137 Views

It my own fault. Never properly declare the variable. Now all the expression can be executed by Matlab function. However, still on the way to determine how to call user defined Matlab function from Fortran.

I thinking of upgrading my fortran compilier to Intel Fortran Compilier V 10.0. Does anyone know whether v10.0 can edit/compilier/run the program without MVS 2005? That is, is it operate independently, or does it need to depend of the MSV2005 to form all tasks?

0 Kudos
anthonyrichards
New Contributor III
1,137 Views

What form is the MatLab function? Is it in C or C++ code? Is it compiled into a linkableobject module? Is it in a DLL? Is it in a static Library? What calling convention does Matlab use?

0 Kudos
anthonyrichards
New Contributor III
1,137 Views

See here for advice http://preview.tinyurl.com/2lxcof

0 Kudos
Reply