FORTRAN & MATLAB 混合編程

0618bianlong@gmail.com算法

第一部分:Fortran調用Matlab引擎

1  什麼是Matlab引擎

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

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

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

★ 從Matlab環境中讀取數據;服務器

★ 向Matlab環境中寫入數據。session

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

注:Fortran程序與在Matlab初次安裝時,會自動執行一次:matlab /regserver將本身在系統的控件庫中註冊。若是由於特殊緣由,沒法打開Matlab引擎,能夠在Dos命令提示符後執行上述命令,從新註冊。app

2  Fortran調用Matlab語句原理

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

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

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

相關文章
相關標籤/搜索