From: George on 9 Jun 2010 01:24 "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hun755$o6n$1(a)fred.mathworks.com>... > "George " <guanjihou(a)gmail.com> wrote in message <hun2m4$7nk$1(a)fred.mathworks.com>... > > "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hummrd$pua$1(a)fred.mathworks.com>... > > > "George " <guanjihou(a)gmail.com> wrote in message <humk70$6jm$1(a)fred.mathworks.com>... > > > > > > > > However, I still have one confusing. The Matlab help stated that mexFunction() is not a function that I can call. I am using fortran, so it is just a subroutine. My question is how can I call this subroutine in my main program? > > > > > > > > Suppose that I have the following main program: > > > > > > > > #include "fintrf.h" > > > > program TestmexCallMatlab > > > > Call mexCallfunction(1,1,1,1) > > > > end > > > > > > > > I tried, but it seems that I can't do this. So could you please tell me how to do this? > > > > > > You can't. mexFunction is a gateway routine that MATLAB calls when you invoke the function from MATLAB. It is not something you call yourself. > > > > > > James Tursa > > > > Thanks James, > > > > So what do I do if I want to call a Matlab function or m file in my main Fortran program? Shall I use the engCallMatlab? > > Yes, you can use the engCallMATLAB function that I have on the FEX since this is not a MATLAB supplied routine. Be aware of some penalties involved, however. > > A mexFunction acts as a subroutine to MATLAB itself. You can basically pass variables back and forth by reference so it is generally quite efficient to call an m-file from within the routine. The downside is you can't use read(*,__) and write(*,__) in your code, you have to use mexPrintf. > > A program has to *copy* everything back & forth between your program and the MATLAB engine, so you incur all of the inefficiencies of the extra data copies floating around in memory as well as the extra time involved in doing the copying. The upside is you can use read(*,__) and write(*,__) in your code. > > James Tursa James, Your explanations help me a lot. I really appreciate. My understanding of your words is that in my Fortran main program I can use engCallMatlab to call a matlab routine, or matlab m file, while in this Matlab routine I can use the compiled mex-files to act as a bridge doing the communication work, right? I have been confused about the difference between these Matlab engine and the Mex-files for quite a long time. Thanks very much, sincerely.
From: George on 9 Jun 2010 01:29 > The pre-processor is case sensitive. So double check that your fintrf.h file to see if mwpointer, MWPOINTER, or mwPointer etc is defined. You can also insert the following: > > #ifndef mwpointer > #define mwpointer integer*4 > #endif > #infndef MWPOINTER > #define MWPOINTER mwpointer > #endif > #ifndef mwPointer > #define mwPointer mwpointer > #endif > > and similar for mwSize. > > What version of MATLAB are you using? > > James Tursa James, Here is my header file #if defined(__x86_64__) || defined(_M_AMD64) || defined(__amd64) || \ defined(__sparcv9) || defined(__ppc64__) # define mwpointer integer*8 # define mwPointer integer*8 # define MWPOINTER INTEGER*8 #else # define mwpointer integer*4 # define mwPointer integer*4 # define MWPOINTER INTEGER*4 #endif And I am using Matlab 2007a, which might be the last version that support Compaq Fortran. So I guess the file should be the most update one. As I cant solve the problem, so I am just using the method I mentioned before instead of this mwpointer. Actually the result should be the same, right?
From: James Tursa on 9 Jun 2010 01:53 "George " <guanjihou(a)gmail.com> wrote in message <hun8r0$fip$1(a)fred.mathworks.com>... > > Here is my header file > > #if defined(__x86_64__) || defined(_M_AMD64) || defined(__amd64) || \ > defined(__sparcv9) || defined(__ppc64__) > # define mwpointer integer*8 > # define mwPointer integer*8 > # define MWPOINTER INTEGER*8 > #else > # define mwpointer integer*4 > # define mwPointer integer*4 > # define MWPOINTER INTEGER*4 > #endif > > And I am using Matlab 2007a, which might be the last version that support Compaq Fortran. So I guess the file should be the most update one. FYI, although it isn't *officially* supported, you can just copy the appropriate mex support files from the 2007a directory to the same directory on some of the later versions and you can still use Compaq Fortran just fine. Eventually I think it does break in the very latest versions because the name mangling of Compaq Fortran is no longer supported. But I haven't actually tried it ... I will have to let you know. > As I cant solve the problem, so I am just using the method I mentioned before instead of this mwpointer. > Actually the result should be the same, right? Yes, as long as integer*4 works for you architecture then using it is OK. However, this is quite confusing. If that macro you show above is in your fintrf.h file then there should be no way that mwPointer is undefined if you include it in your code. Do you have multiple source files? The fintrf.h would need to be included in all of them. That is the only explanation I can think of at the moment for why you might be getting the error. James Tursa
From: James Tursa on 9 Jun 2010 02:09 "George " <guanjihou(a)gmail.com> wrote in message <hun8hl$qqv$1(a)fred.mathworks.com>... > > My understanding of your words is that in my Fortran main program I can use engCallMatlab to call a matlab routine, or matlab m file ... Yes. > ... while in this Matlab routine I can use the compiled mex-files to act as a bridge doing the communication work, right? The MATLAB engine can call mex routines, but I don't know what you mean by "bridge doing the communication work". > I have been confused about the difference between these Matlab engine and the Mex-files for quite a long time. Think of a mex routine as a subroutine that gets attached directly to MATLAB. So mex function calls etc happen through a direct interface to the routine ... only pointers to the variables are passed so it is very efficient. The prhs(*) pointers you get in the mexFunction argument list point directly at the MATLAB workspace variables. There is no extra copying involved, and you can access the contents of these workspace variables directly in your code. Likewise from within your mex routine you can make direct calls to MATLAB functions or m-files via the mexCallMATLAB routine ... no extra data copies are involved as you are only passing mxArray pointers to the routine. Think of the relationship between a program and a MATLAB engine as two completely separate programs running that have no direct communication with each other. Instead, they have to use a third party to talk to each other ... e.g., a COM interface. So to call an m-file from the program you have to first create an mxArray version of the variable you want to pass (maybe an extra copy), then pass that to the COM interface (via the engPutVariable routine), which causes another copy to be made on the MATLAB engine side. Then you can invoke the m-file via a engEvalString function call. Then to get the result back into your program you need to use engGetVariable which uses the COM interface and makes a copy of the MATLAB engine workspace output variable in your Fortran code. Then maybe there is another copy involved to get the data into a regular Fortran variable. That's a lot of extra work in the background to call the m-file, but it can be done and it can be quite useful to use built-in MATLAB functions or m-files this way in spite of the extra data movement. James Tursa
From: George on 9 Jun 2010 12:48 "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <huna81$e9m$1(a)fred.mathworks.com>... > "George " <guanjihou(a)gmail.com> wrote in message <hun8r0$fip$1(a)fred.mathworks.com>... > > > > Here is my header file > > > > #if defined(__x86_64__) || defined(_M_AMD64) || defined(__amd64) || \ > > defined(__sparcv9) || defined(__ppc64__) > > # define mwpointer integer*8 > > # define mwPointer integer*8 > > # define MWPOINTER INTEGER*8 > > #else > > # define mwpointer integer*4 > > # define mwPointer integer*4 > > # define MWPOINTER INTEGER*4 > > #endif > > > > And I am using Matlab 2007a, which might be the last version that support Compaq Fortran. So I guess the file should be the most update one. > > FYI, although it isn't *officially* supported, you can just copy the appropriate mex support files from the 2007a directory to the same directory on some of the later versions and you can still use Compaq Fortran just fine. Eventually I think it does break in the very latest versions because the name mangling of Compaq Fortran is no longer supported. But I haven't actually tried it ... I will have to let you know. > > > As I cant solve the problem, so I am just using the method I mentioned before instead of this mwpointer. > > Actually the result should be the same, right? > > Yes, as long as integer*4 works for you architecture then using it is OK. However, this is quite confusing. If that macro you show above is in your fintrf.h file then there should be no way that mwPointer is undefined if you include it in your code. Do you have multiple source files? The fintrf.h would need to be included in all of them. That is the only explanation I can think of at the moment for why you might be getting the error. > > James Tursa James, Yes, it is really confusing. I am sure I just have one source file, and I double check the header file, it is as follow: #if defined(WITH_COMMENTS) /* * fintrf.h - MATLAB/FORTRAN interface header file. This file * contains the declaration of the pointer type needed * by the MATLAB/FORTRAN interface. * * Copyright 1984-2006 The MathWorks, Inc. * All Rights Reserved. */ #endif #if defined(__x86_64__) || defined(_M_AMD64) || defined(__amd64) || \ defined(__sparcv9) || defined(__ppc64__) # define mwpointer integer*8 # define mwPointer integer*8 # define MWPOINTER INTEGER*8 #else # define mwpointer integer*4 # define mwPointer integer*4 # define MWPOINTER INTEGER*4 #endif #if defined(MX_COMPAT_32) # define mwsize integer*4 # define mwSize integer*4 # define MWSIZE INTEGER*4 # define mwindex integer*4 # define mwIndex integer*4 # define MWINDEX INTEGER*4 #else # define mwsize mwpointer # define mwSize mwpointer # define MWSIZE MWPOINTER # define mwindex mwpointer # define mwIndex mwpointer # define MWINDEX MWPOINTER #endif And this is part of my source code: #include "fintrf.h" C #if 0 C C test.F C .F file need to be preprocessed to generate .for equivalent C #endif C C test.f C C This is a simple program that plots a sine wave C C Copyright 1984-2005 The MathWorks, Inc. C====================================================================== program main C----------------------------------------------------------------------- C (pointer) Replace integer by integer*8 on 64-bit platforms C mwpointer engOpen mwpointer ep C---------------------------------------------------------------------- C C Other variable declarations here integer engPutVariable, engEvalString, engClose integer temp, status C ep = engOpen('matlab ') C if (ep .eq. 0) then write(6,*) 'Can''t start MATLAB engine' stop endif end With this error: : Error: Syntax error, found END-OF-STATEMENT when expecting one of: ( : % . = => mwpointer engOpen -----------------------^ : Error: Syntax error, found END-OF-STATEMENT when expecting one of: ( : % . = => mwpointer ep ------------------^ : Error: This statement is positioned incorrectly and/or has syntax errors. mwpointer ep It is rather strange. Thanks.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: 2d interpolation of an image Next: how i can calculate inverse TFD (log (Matrix(k, n))) |