FORTRAN & MATLAB 混合編程


0618bianlong@gmail.com

第一部分:Fortran調用Matlab引擎

1  什么是Matlab引擎

所謂Matlab引擎(engine),是指一組Matlab提供的接口函數,支持C/C++、Fortran等語言,通過這些接口函數,用戶可以在其它編程環境中實現對Matlab的控制。可以主要功能有:

★ 打開/關閉一個Matlab對話;

★ 向Matlab環境發送命令字符串;

★ 從Matlab環境中讀取數據;

★ 向Matlab環境中寫入數據。

與其它各種接口相比,引擎所提供的Matlab功能支持是最全面的。通過引擎方式,應用程序會打開一個新的Matlab進程,可以控制它完成任何計算和繪圖操作。對所有的數據結構提供100%的支持。同時,引擎方式打開的Matlab進程會在任務欄顯示自己的圖標,打開該窗口,可以觀察主程序通過engine方式控制Matlab運行的流程,並可在其中輸入任何Matlab命令。

注:Fortran程序與在Matlab初次安裝時,會自動執行一次:matlab /regserver將自己在系統的控件庫中注冊。如果因為特殊原因,無法打開Matlab引擎,可以在Dos命令提示符后執行上述命令,重新注冊。

2  Fortran調用Matlab語句原理

實際上,Fortran通過引擎函數調用MATLAB,實質是把Matlab作為一個ActiveX服務器,也叫Matlab計算引擎。當Fortran程序調用某個MATLAB函數或命令時,首先通過引擎函數啟動Matlab並建立ActiveX通道,然后把這個函數或命令通過ActiveX通道傳給Matlab,由Matlab在后台執行(這可以分成兩步來完成:第一步將mxArray轉換成Matlab可理解的形式。用mxCreate來創建一個和要傳遞的數據類型大小相同的矩陣mxArray;第二步將矩陣放入Matlab工作區中,用程序engPutVariable和engEvalString來完成。)。

下面給出幾個比較重要的Fortran程序可以調用的引擎函數的功能說明,其他函數可以通過Matlab幫助找到:

engOpen:啟動MATLAB引擎,建立ActiveX通道;

engClose:關閉MATLAB引擎,即關閉ActiveX通道;

engGetMatrix:從MATLAB引擎得到MATLAB數組值;

engPutMatrix:把一個MATLAB數組傳給MATLAB引擎;

engEvalString:執行一個MATLAB命令;

engOutputBuffer:建立一個緩沖區以儲存MATLAB的文本輸出。

當然,要實現Matlab函數或命令的調用執行,除了調用Matlab語言的引擎函數外,還需要調用Matlab提供的API(應用程序接口)函數。由於Fortran與Matlab語言的計算單元不一樣,前者以數位運算單位,而后者以矩陣(mxArray結構體)為基本的運算單位,導致運算的數據類型不能直接匹配。Matlab API函數采用獨特的mxArray數據類型進行運算,能將Fortran程序運行的雙精度實型變量轉化成矩陣類型;也可以將Matlab矩陣類型變化成Fortran程序運行的雙精度實型變量。

Fortran調用Matlab引擎編程步驟如下:

(1) 在Fortran編譯器中聲明引擎函數,API函數等。

(2) 在Fortran編譯器中定義指針。在Matlab與Fortran混合編程中,指針是傳遞數據的唯一方式。因此,每一個傳遞到Matlab中的Fortran數據都需要一個指針來指向其在Matlab中對應的mxArray結構體。

(3) 通過Matlab引擎函數啟動Matlab引擎,讓Matlab在后台開始運行。

(4) 創建Matlab矩陣,將Fortran變量傳遞給Matlab矩陣。

(5) 執行所需的Matlab命令完成運算、函數調用、繪圖等。

(6) 將Matlab運算結果返回給Fortran程序。

3  編譯器設置

3.1  Compaq Visual Fortran 6.6中的設置

(1)設置頭文件目錄

在tool->options->Directories里的Include files添加(假設D:\Program Files\MATLAB是Matlab安裝路徑):

D:\Program Files\MATLAB\R2008a\extern\include

(2)設置庫文件目錄

Library files里添加(假設D:\Program Files\MATLAB是Matlab安裝路徑):

D:\Program Files\MATLAB\R2008a\extern\lib\win32\microsoft

(3)添加鏈接庫輸入項

在project->setting->link->Object/library modules添加libmx.lib libeng.lib

★ 注意*.lib文件之間用空格分隔,不可用逗號分隔。

(1)和(2)設置一次就可以,而(3)是每新建一個Project都要重新設置。也可以在Compaq Visual Fortran6.6中可以保存(3)中的設置,方法:點擊菜單Files->Save Fortran Environment,在Save Fortran Console Environment as:輸入一個名字,點擊Save Environment。這樣就可以在以后直接使用這一編譯環境,例如:在新建一個Project后會彈出對話框”Would you like to consider applying options from a Saved Fortran Console Environment?”點擊”是(Y)”,在對話框中List of Console Environment中,選擇前面save過的環境名稱,點擊”Apply”按鈕並確認,在接下來的對話框中點擊”OK”。

3.2  Intel Visual Fortran 10.1中的設置

(1)設置頭文件目錄

在菜單中選擇工具->選項,在對話框左邊選擇Intel(R) Fortran->Compilers,在右邊的Includes里添加:D:\Program Files\MATLAB\R2008a\extern\include,如圖1。

 

圖  1

(2)設置庫文件目錄

同理,在右邊的Libraries里添加:

D:\Program Files\MATLAB\R2008a\extern\lib\win32\microsoft

(3)添加鏈接庫輸入項

在解決方案資源管理器里選擇對應的項目單機右鍵選擇最下面的“屬性”彈出屬性對話框,在右邊選擇Linker->Input,在Additional Dependencies里添加:libeng.lib libmx.lib,如圖2。

★ 注意:*.lib文件之間用空格分隔,不可用逗號分隔。

 

圖  2

(4)設置環境變量

現在生成時就沒問題出現了,但是運行程序時可能會出現“因為計算機中丟失libeng.dll”等類似問題。第(4)步就解決這個問題的,如圖3。

把D:\Program Files\MATLAB\R2008a\bin\win32;添加到PATH里邊(如果是64位系統則添加D:\Program Files\MATLAB\R2008a\bin\win64)。

在Win7下具體可以這樣做:在桌面上選擇“計算機”圖標,右鍵彈出選單,在其中選擇“屬性”,然后在彈出的窗口中選中左邊的高級系統設置,在屬性對話框里選擇“高級”選項卡,載選擇“環境變量”,在新對話框里的系統環境下邊找到PATH這一項選中,然后編輯,在后邊添加

D:\Program Files\MATLAB\R2008a\bin\win32;

64位系統選擇

D:\Program Files\MATLAB\R2008a\bin\win64;

 

圖  3

★ 注意:不要刪除path里原有的其他路徑。

如果設置環境變量前打開了visual studio ,那么需要重啟visual studio。

第二部分:Matlab調用Fortran程序

1  創建MEX文件實現對Fortran的調用

1.1  MEX文件介紹

Matlab MEX文件是Matlab系統的外部程序調用接口。MEX文件是由C/Fortran語言編寫的,編譯后生成Matlab動態鏈接子程序,可在Matlab環境下導入和執行,如同Matlab的內置函數一樣。這樣可以使用Fortran語言進行算法設計,然后再Matlab環境下調用,提高Matlab環境中數據處理效率,主要應用有:對已有的C/Fortran程序,可通過MEX方式在Matlab環境中直接調用;對影響Matlab執行速度的FOR循環,可以編寫相應的C/Fortran子程序完成相同功能,並編譯成MEX,提高運行速度。

MEX文件實際上為一種Matlab專用的動態鏈接庫文件。

1.2  MEX文件調用的基本原理

Matlab和Fortran語言的運算單位不同,Matlab以矩陣(mxArray結構體)為基本數據結構,而在Fortran中,文件是由按順序排列的記錄組成的,而記錄是數值或字符的序列,是Fortran程序輸入輸出的基本單位。記錄有兩種格式:格式記錄和無格式記錄。格式記錄中,數據在文件中的存放是用ASCII碼形式;無格式記錄中,數據在文件中存放是用二進制代碼形式。由於Fortran和Matlab的數據類型不能直接匹配,故需調用Matlab的API函數來完成二者的數據轉換,其基本原理是:Matlab將需要傳遞的mxArray型數據的內存地址作為一個整型數值傳遞給Fortran程序,然后在Fortran中,API提供的訪問函數(access routiness)使用此整數值來訪問mxArray的內容,並將此值作為內存地址,讀取相應內容。

1.3  Fortran語言MEX文件源程序的構成

Fortran MEX文件通常由兩個顯著不同的部分組成:(1)計算子程序,是完成計算功能的純Fortran程序,通常為現有的Fortran程序代碼;(2)入口子程序,是計算子程序與Matlab環境之間的接口,用來完成相互之間的調用。

計算子程序實際上被入口子程序當作子程序來調用,如果非常簡單,也可以直接嵌入到入口子程序中,但為了保持可讀性,一般不建議這么做。

入口子程序名字為mexFunction,包含4個虛擬參數:

prhs,輸入參數的mxArray類型指針;

nrhs,輸入參數個數;

plhs,輸出參數的mxArray類型指針;

nlhs,輸出參數個數。

調用格式為:

subroutine mexFunction ( nlhs, plhs, nrhs, prhs)

入口子程序通過MEX文件的API函數與Matlab進行數據交換。

★ 注意:其實上述輸入、輸出參數的名稱非常容易記憶,lhs代表Left hand parameters,rhs代表right hand parameters,n代表number,p代表pointer。這里的左手和右手如何划分呢?例如在MATLAB中調用求伯特圖的函數時,可以這樣調用:

[mag,phase,w] = bode(sys)

這里以“=”為分界,左邊的參數mag、phase和w為輸出參數,即左手參數,而等號右邊的參數sys,為輸入參數,即右手參數。

1.4  編譯器配置

在Matlab命令提示符下鍵入配置命令:mex  -setup,並按提示進行操作逐步完成。

★:只用mex -setup一次即可,以后運行mex文件等都不用再mex -setup。

1.5  MEX文件的執行流程

以由計算子程序和入口子程序組成的plus.for文件為例。首先,在Matlab環境下輸入命令:mex plus.for;即對Fortran文件進行編譯。編譯完成后會創建出Matlab的MEX文件,即該目錄下出現plus.mexw32文件,只需正確的鍵入該MEX文件名及其所需參數即可使用,例如z=plus(x, y)。可以在Matlab命令行下鍵入what指令查看當前路徑下是否具有MEX文件。

1.6  %val結構

如果你的FORTRAN編譯器支持%val結構(如Fortran90),有一種不要入口路徑的指針類型,直接用mxGetPr或mxGetPi來取得數據指針,再用%val把指針內容傳給Fortrran子程序,在這里把它作為一個FORTRAN的雙精度矩陣,這種傳遞稱為“值傳遞”。如果你的編譯器不支持%val結構,就必須采用mxCopy路線(如mxCopyPtrToReal8)獲得指針的內容。

%Val結構的定義及語法:

Built-in Function: Changes the form of an actual argument. Passes the argument as an immediate value.

Syntax

result = %VAL (a)

a (Input): An expression, record name, procedure name, array, character array section, or array element.

You must specify %VAL in the actual argument list of a CALL statement or function reference. You cannot use it in any other context.

例,不使用%val結構(Fortran 77)寫法:

y_pr=mxGetPr(plhs(1))

x_pr=mxGetPr(prhs(1))

call mxCopyPtrToReal8(x_pr,x,size)

call times2(size,x,y)

call mxCopyReal8ToPtr(y,y_pr,size)

使用%val結構(Fortran 90)寫法:

y_pr=mxGetPr(plhs(1))

x_pr=mxGetPr(prhs(1))

call times2(size,%val(x_pr),%val(y_pr))

1.7  Fortran語言MEX文件中對Matlab函數的調用

在Fortran語言MEX文件中,用戶不但可以調用Fortran語言的內建函數,而且還可以通過API提供的子程序mexCallMATLAB來完成對Matlab內建函數、運算符、M文件的調用。

 

 

 

2  創建DLL文件實現對Fortran的調用

2.1  DLL文件介紹

動態鏈接庫(Dynamic Link Library,DLL)是一個包含可由多個程序同時使用的代碼和數據的庫。動態鏈接提供了一種方法,使進程可以調用不屬於其可執行代碼的函數。函數的可執行代碼位於一個DLL中,該DLL包含一個或多個已被編譯、鏈接並與使用它們的進程分開存儲的函數。多個應用程序可同時訪問內存中單個DLL副本的內容。使用動態鏈接庫具有如下優點:擴展了應用程序的特性;簡化了軟件項目的管理與升級;有助於節省內存、實現資源共享;可以用多種編程語言來編寫。

2.2  Matlab調用Fortran動態連接庫

混合語言程序設計中的主要問題:如何在編程時遵循不同語言中變量和過程的命名約定、堆棧使用約定以及函數調用過程中的參數傳遞約定。其中堆棧約定確定子過程的參數數目是否可變以及何時進行調用后的清理堆棧工作;命名約定確定標識符是否對大小寫敏感及編譯后對標識符如何修飾;參數約定確定參數傳遞時是傳值方式還是傳址(引用)方式及不同語言之間的數據類型和數據結構如何對應。Fortran語言調用約定有三種,即:STDCALL約定、C約定和Default調用約定。

Matlab可由兩種方式調用Fortran語言編寫的動態鏈接庫。第一種方式是利用Matlab生成的MEX文件,即第二部分第1章節的內容;第二種方式是利用Fortran編譯器,將Fortran語言編寫的函數編譯成通用32位的動態鏈接庫(后綴名為dll),Matlab通過幾個專門的API接口函數調用。

2.3  DLL文件的生成

生成動態鏈接庫具體步驟如下:

(1)創建一個Fortran Dynamic Link Library工程。

(2)在工程中添加已經寫好的子程序dll_test.for文件。

(3)在dll_test.for文件中,subroutine add(x,y,z)之后插入偽注釋語句:

!DEC$ ATTRIBUTES C,DLLEXPORT :: add

!DEC$ ATTRIBUTES REFERENCE ::z

第一句表明庫函數add在動態鏈接庫外可被調用,並按C調用約定,只有聲明了DLLEXPORT屬性,才能從DLL中導出過程,該過程也才能為外部程序所調用。第二句指定z為引用傳遞。也可以指定z為值傳遞,即

!DEC$ ATTRIBUTES VALUE ::z

★ 調用約定規定了過程中所有參數的傳遞,而REFERENCE和VALUE屬性則最終決定參數的傳遞(在任何情況下,數組參數只能以引用方式進行傳遞)。

(4)編譯生成dll_test.DLL動態鏈接庫文件。

(5)編寫和C語言等效的DLL輸出函數定義頭文件dll_test.h,內容如下:

void add(int,int,int*);回車

void表示“空”,無返回值,int定義對應的變量為整型變量,int*代表指針類型,表示指向整型變量的指針。

★  注意要有回車,語句下要有空行。

2.3  DLL動態鏈接庫的調用

Matlab利用三個內部函數loadlibrary、calllib和unloadlibrary調用Fortran成的通用DLL動態鏈接庫,具體步驟如下:

(1)利用loadlibrary加載動態鏈接庫及對應頭文件。

(2)利用calllib調用動態鏈接庫中的輸出函數。

(3)利用unloadlibrary釋放動態鏈接庫。

在Matlab中編寫m文件:

clc

clear

hfile='dll_test.h';

loadlibrary('dll_test.dll', hfile);     %加載dll及對應的頭文件

x=0;

result=calllib('dll_test', 'add', 3,5,x);  %matlab里的calllib函數返回

disp('result = '),disp(result)

unloadlibrary dll_test

第三部分:函數說明

1  引擎函數功能說明

1.1  engOpen:啟動MATLAB引擎,建立ActiveX通道。

Fortran Syntax(語法):

Integer*4 function engOpen(startcmd)

Integer*4 ep

Character*(*) startcmd

Arguments(參數):

Startcmd:String to start the MATLAB process. On Windows systems, the startcmd string must be NULL.

Returns(返回值):

A pointer to an engine handle or NULL if the open fails.

1.2  engClose:關閉MATLAB引擎,即關閉ActiveX通道。

Fortran Syntax(語法):

Integer*4 function engClose(ep)

Integer*4 ep

Arguments(參數):

Ep:Engine pointer

Returns(返回值):

0 on success, and 1 otherwise. Possible failure includes attempting to terminate a MATLAB engine session that was already terminated.

1.3  engPutMatrix為舊的寫法,現替換為engPutVariable: 把一個變量傳給MATLAB引擎。

Fortran Syntax(語法):

Integer*4 function engPutVariable(ep,name,pm)

Integer*4 ep,pm

Character*(*) name

Arguments(參數):

Ep:Engine pointer

Name:Name given to the mxArray in the engine's workspace

Pm:mxArray pointer

Returns(返回值):

0 if successful and 1 if an error occurs

1.4  engGetMatrix為舊的寫法,現替換為engGetVariable: 從MATLAB引擎得到變量值。

Fortran Syntax(語法):

Integer*4 function engGetVariable(ep,name)

Integer*4 ep

Character*(*) name

Arguments(參數):

Ep:Engine pointer

Name: Name of mxArray to get from MATLAB workspace

Returns(返回值):

A pointer to a newly allocated mxArray structure, or NULL if the attempt fails. engGetVariable fails if the named variable does not exist.

1.5  engEvalString: 執行一個MATLAB命令。

Fortran Syntax(語法):

Integer*4 function engEvalString(ep,string)

Integer*4 ep

Character*(*) string

Arguments(參數):

Ep:Engine pointer

String: String to execute

Returns(返回值):

0 if the command was evaluated by the MATLAB engine session, and nonzero otherwise. Possible reasons for failure include the MATLAB engine session is no longer running or the engine pointer is invalid or NULL.

★ engEvalString也可以當subroutine使用,例如call engEvalString(ep,string)。

 

1.6  engOutputBuffer:建立一個緩沖區以儲存Matlab的文本輸出。

Fortran Syntax(語法):

Integer*4 function engOutputBuffer(ep,p)

Integer*4 ep

Character*n p

Arguments(參數):

Ep:Engine pointer

P:pointer to character buffer

N:length of buffer p

Returns(返回值):

1 if you pass it a NULL engine pointer. Otherwise, it returns 0.

2  mx函數功能說明

Mx function功能:操作mxArray矩陣(MX Array Manipulation)。

2.1  mxCreateFull為舊的寫法,現替換為mxCreateDoubleMatrix: 創建一個二維雙精度矩陣。

Fortran Syntax(語法):

Integer*4 function mxCreateDoubleMatrix(m, n, Complexflag)

Integer*4 m, n, Complexflag

Arguments(參數):

m: The desired number of rows

n: The desired number of columns

Complexflag: If the data you plan to put into the mxArray has no imaginary components, specify 0. If the data has some imaginary components, specify 1.

Returns(返回值):

A pointer to the created mxArray, if successful. If unsuccessful returns 0.

2.2  mxCopyReal8ToPtr:復制Fortran中雙精度實型數組的值到矩陣。

Fortran Syntax(語法):

subroutine mxCopyReal8ToPtr(y, px, n)

real*8 y(n)

integer*4 px, n

Arguments(參數):

y: real*8 Fortran array

px: Pointer to the real or imaginary data of a double-precision Matlab array

n: Number of elements to copy.

Description:

mxCopyReal8ToPtr copies n REAL*8 values from the Fortran REAL*8 array y into the MATLAB array pointed to by px, either a pr or pi array.

2.3  mxCopyPtrToReal8:把Matlab中的矩陣傳給Fortran中的雙精度實型數組。

Fortran Syntax(語法):

subroutine mxCopyPtrToReal8 (px, y, n)

real*8 y(n)

integer*4 px, n

Arguments(參數):

y: real*8 Fortran array

px: Pointer to the real or imaginary data of a double-precision Matlab array

n: Number of elements to copy.

Description:

mxCopyPtrToReal8 copies n REAL*8 values from the MATLAB array pointed to by px, either a pr or pi array, into the Fortran REAL*8 array.

2.4  mxCopyComplex16ToPtr:復制Fortran中復型數組的值到矩陣。

Fortran Syntax(語法):

subroutine mxCopyComplex16ToPtr(y, pr, pi, n)

complex*16 y(n)

integer*4 pr, pi, n

Arguments(參數):

y: complex*16 Fortran array

pr: Pointer to the real data of a double-precision Matlab array

pi: Pointer to the imaginary data of a double-precision Matlab array

n: Number of elements to copy.

Description:

mxCopyComplex16ToPtr copies n COMPLEX*16 values from the Fortran COMPLEX*16 array y into the MATLAB arrays pointed to by pr and pi. This subroutine is essential for use with Fortran compilers that do not support the %VAL construct in order to set up standard Fortran arrays for passing as arguments to the computation routine of a MEX-file..

2.5  mxCopyPtrToComplex16:把Matlab中的矩陣傳給Fortran中的復型數組。

Fortran Syntax(語法):

subroutine mxCopyPtrToComplex16 (pr, pi, y, n)

complex*16 y(n)

integer*4 pr, pi, n

Arguments(參數):

y: complex*16 Fortran array

pr: Pointer to the real data of a double-precision Matlab array

pi: Pointer to the imaginary data of a double-precision Matlab array

n: Number of elements to copy.

Description:

mxCopyPtrToComplex16 copies n COMPLEX*16 values from the MATLAB arrays pointed to by pr and pi into the Fortran COMPLEX*16 array y. This subroutine is essential for use with Fortran compilers that do not support the %VAL construct in order to set up standard Fortran arrays for passing as arguments to the computation routine of a MEX-file.

2.6  mxGetM:Get number of rows in mxArray.

Fortran Syntax(語法):

Integer*4 function mxGetM(pm)

Integer*4 pm

Arguments(參數):

Pm: Pointer to an mxArray

Returns(返回值):

The number of rows in the mxArray to which pm points.

2.7  mxGetN:Get number of columns in mxArray.

Fortran Syntax(語法):

Integer*4 function mxGetN(pm)

Integer*4 pm

Arguments(參數):

Pm: Pointer to an mxArray

Returns(返回值):

The number of columns in the mxArray.

 

2.8  mxGetPr:得到mxArray中的實型數據。

Fortran Syntax(語法):

Integer*4 function mxGetPr(pm)

Integer*4 pm

Arguments(參數):

pm: Pointer to an mxArray

Returns(返回值):

The address of the first element of the real data. Returns 0 if there is no real data. Once you have the starting address, you can access any other element in the mxArray.

2.9  mxGetPi:得到mxArray中的虛部數據。

Fortran Syntax(語法):

Integer*4 function mxGetPi(pm)

Integer*4 pm

Arguments(參數):

pm: Pointer to an mxArray

Returns(返回值):

The imaginary data elements of the specified mxArray, on success. Returns 0 in Fortran if there is no imaginary data or if there is an error.

2.10  mxGetString: copy string mxArray into character array in Fortran.

Fortran Syntax(語法):

integer*4 function mxGetString(pm, str, strlen)

integer*4 pm, strlen

character*(*) str

Arguments(參數):

pm: Pointer to a string mxArray

str: The starting location into which the string should be written.

strlen: Maximum number of characters to read into str.

Returns(返回值):

0 on success, and 1 on failure.

2.11  mxGetScalar: Get real component of first data element in mxArray.

Fortran Syntax(語法):

real*8 function mxGetScalar(pm)

integer*4 pm

Arguments(參數):

pm: Pointer to an mxArray; cannot be a cell mxArray, a structure mxArray, or an empty mxArray.

Returns(返回值):

The value of the first real (nonimaginary) element of the mxArray.

2.12  mxCreateString: Create 1-by-N string mxArray initialized to specified string.

Fortran Syntax(語法):

integer*4 function mxCreateString(str)

character*(*) str

Arguments(參數):

str: The string that is to serve as the mxArray's initial data.

Returns(返回值):

A pointer to the created string mxArray if successful, and 0 in Fortran otherwise.

2.13  mxIsNumeric: Determine whether mxArray is numeric.

Fortran Syntax(語法):

Integer*4 function mxIsNumeric(pm)

Integer*4 pm

Arguments(參數):

Pm: Pointer to an mxArray

Returns(返回值):

Logical 1 (true) if the array can contain numeric data. Logical 0 (false) if the array cannot contain numeric data.

2.14  mxIsDouble: Determine whether mxArray represents data as double-precision, floating-point numbers.

Fortran Syntax(語法):

Integer*4 function mxIsDouble(pm)

Integer*4 pm

Arguments(參數):

Pm: Pointer to an mxArray

Returns(返回值):

Logical 1 (true) if the mxArray stores its data as double-precision, floating-point numbers, and logical 0 (false) otherwise.

2.15  mxIsComplex: Determine whether data is complex.

Fortran Syntax(語法):

Integer*4 function mxIsComplex(pm)

integer*4 pm

Arguments(參數):

pm: Pointer to an mxArray

Return:

Logical 1 (true) if pm is a numeric array containing complex data, and logical 0 (false) otherwise. If pm points to a cell array or a structure array, mxIsComplex returns false.

2.16  mxIsChar: Determine whether input is string mxArray.

Fortran Syntax(語法):

Integer*4 function mxIsChar(pm)

integer*4 pm

Arguments(參數):

pm: Pointer to an mxArray

Return:

Logical 1 (true) if pm points to an array having the class mxCHAR_CLASS, and logical 0 (false) otherwise.

 

2.17  mxDestroyArray:釋放了由mxCreate函數創建的動態內存。

Fortran Syntax(語法):

subroutine mxDestroyArray(pm)

integer*4 pm

Arguments(參數):

pm: Pointer to the mxArray you want to free

Description:

mxDestroyArray deallocates the memory occupied by the specified mxArray. mxDestroyArray not only deallocates the memory occupied by the mxArray's characteristics fields (such as m and n), but also deallocates all the mxArray's associated data arrays.

3  mex函數功能說明

Mex function:即編寫MEX文件的API函數。

3.1  mexFunction: MEX文件的入口函數,當在MATLAB命令行中執行MEX函數時,MATLAB解釋器將從此函數處開始執行MEX代碼。

Fortran Syntax(語法):

mexFunction(nlhs, plhs, nrhs, prhs)

integer*4 nlhs, nrhs

integer*4 plhs(*), prhs(*)

Arguments(參數):

prhs,輸入參數的mxArray類型指針;

nrhs,輸入參數個數;

plhs,輸出參數的mxArray類型指針;

nlhs,輸出參數個數。

Returns(返回值):

mexFunction is not a routine you call. Rather, mexFunction is the name of a subroutine in Fortran that you must write in every MEX-file. When you invoke a MEX-function, MATLAB® software finds and loads the corresponding MEX-file of the same name. MATLAB then searches for a symbol named mexFunction within the MEX-file. If it finds one, it calls the MEX-function using the address of the mexFunction symbol. If MATLAB cannot find a routine named mexFunction inside the MEX-file, it issues an error message.

3.2  mexErrMsgTxt: Issue error message and return to Matlab prompt.

Fortran Syntax(語法):

mexErrMsgTxt(errormsg)

character*(*) errormsg

Arguments(參數):

Errormsg: String containing the error message to be displayed

Returns(返回值):

Call mexErrMsgTxt to write an error message to the MATLAB window. After the error message prints, MATLAB terminates the MEX-file and returns control to the MATLAB prompt.

Calling mexErrMsgTxt does not clear the MEX-file from memory. Consequently, mexErrMsgTxt does not invoke the function registered through mexAtExit.

If your application called mxCalloc or one of the mxCreate* routines to allocate memory, mexErrMsgTxt automatically frees the allocated memory.

In addition to the errormsg, the mexerrmsgtxt function determines where the error occurred, and displays the following information. If an error labeled Print my error message occurs in the function foo, mexerrmsgtxt displays:

??? Error using ==> foo

Print my error message

3.3  mexPrintf: Prints a string on the screen and in the diary (if the diary is in use).

Fortran Syntax(語法):

integer*4 mexPrintf(message)

character*(*) message

Arguments(參數):

message: String to be displayed.

Returns(返回值):

The number of characters printed. This includes characters specified with backslash codes, such as \n and \b.

3.4  mexAtExit: Register function to call when MEX-function is cleared or MATLAB software terminates.

mexAtExit函數可以用來向Matlab注冊函數,注冊的函數將在MEX文件從內存中清除時被自動調用,從而可以完成一些清除內存,釋放空間的操作。

Fortran Syntax(語法):

integer*4 function mexAtExit(ExitFcn)

subroutine ExitFcn

Arguments(參數):

ExitFcn: Pointer to function you want to run on exit.

Returns(返回值):

Always returns 0. In Fortran, you must declare the ExitFcn as external in the Fortran routine that calls mexAtExit if it is not within the scope of the file.

3.5  mexIsLocked: Determine whether MEX-file is locked.

Fortran Syntax(語法):

integer*4 function mexIsLocked()

Returns(返回值):

Logical 1 (true) if the MEX-file is locked; logical 0 (false) if the file is unlocked.

3.6  mexLock: 鎖定函數。Prevent MEX-file from being cleared from memory.

Fortran Syntax(語法):

mexLock()

Description:

By default, MEX-files are unlocked, meaning that a user can clear them at any time. Call mexLock to prohibit a MEX-file from being cleared.

3.7  mexUnlock: 解鎖函數。Allow MEX-file to be cleared from memory.

Fortran Syntax(語法):

mexUnlock()

Description:

By default, MEX-files are unlocked, meaning that a user can clear them at any time.

3.8  mexCallMATLAB: Call MATLAB function or user-defined M-file or MEX-file.

Fortran Syntax(語法):

mexCallMATLAB(nlhs, plhs, nrhs, prhs, name)

integer*4 nlhs, nrhs

integer*4 plhs(*), prhs(*)

character*(*) name

Arguments(參數):

prhs,輸入參數的mxArray類型指針;

nrhs,輸入參數個數;

plhs,輸出參數的mxArray類型指針;

nlhs,輸出參數個數。

name, Character string containing the name of the MATLAB built-in, operator, M-file, or MEX-file that you are calling.

★ 注意:這里的nrhs,prhs,nlhs,plhs是真對name函數(或m文件)的輸入輸出參數,而不是整個接口函數mexFunction的輸入輸出參數。

Returns(返回值):

0 if successful, and a nonzero value if unsuccessful.

 

Function

Successful

Unsuccessful

engOpen

非0

0

engClose

0

1

engEvalString

0

非0

engPutVariable

0

1

engGetVariable

非0

0

mxCreateDoubleMatrix

非0

0

mxGetPr

非0

0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM