乙個MEX程式示例

2021-07-11 03:50:37 字數 939 閱讀 5224

mex檔案是由c或fortran語言編寫的源**,經matlab編譯器處理而產生的二進位制檔案。它可以被matlab直譯器自動裝載並執行的動態鏈結程式,類似windows下的dll檔案。mex檔案實現了c語言與matlab的介面,以下例子闡述該介面。

#include "mex.h" 

#include double getloglike(int size, double* x, double* mu, double* sd){

for (int i = 0; i

getloglike是計算似然的乙個c函式。

mexfunction是介面函式,

void mexfunction ( int nlhs, mxarray *plhs, int nrhs, const mxarray *prhs )

返回型別:void

int nlhs:返回的引數的個數

mxarray *plhs:返回的引數對應的指標組成的陣列

int nrhs:輸入的引數的個數

const mxarray *prhs:輸入的引數的對應的指標組成的陣列

函式功能介紹:

mxgetpr(a), 得到資料a裡存放的data的首位址

mxgetm(a), 得到資料a裡存放的行值

mxgetn(a), 得到資料a裡存放的列值

plhs[0] = mxcreatedoublematrix(m,n,mxreal), 分配乙個m*n的matlab矩陣,其位址賦給plhs[0]

*a = getloglike(n, m0, m1, m2);對相應的指標呼叫c函式進行計算。

了位址傳遞,所以對應的sd值在每次呼叫後值發生變化,而在用matlab函式實現該功能時sd值是採用值傳遞,

並不希望sd值變化,所以在編寫mex檔案時,注意位址傳遞和值傳遞的方式,保證資料不能意外更改。

乙個C 程式的示例 STATIC,FRIEND

include using namespace std class myclass static char str static void print void friend void printprivate myclass myclass private char mchar char mycl...

乙個PlaySound示例

using system using system.collections using system.componentmodel using system.runtime.interopservices internal class helpers dllimport winmm public s...

xml的乙個示例

function.h cstring openfile cstring cstring getcontext cstring,int,int void str2array cstring struct xmlnode function.cpp include include include incl...