精華區beta MATLAB 關於我們 聯絡資訊
在繼續走下去之前, 先打開 Matlab, 輸入 helpwin 關於這個主題, Matlab 提供的說明在... Contents/ MATLAB/ External Interfaces/ Calling C and Fortran Programs from MATLAB Creating C Language MEX-Files 上述兩個章節跟這個主題的關係很密切 Matlab的 help 寫得相當好 有問題都可以在這找到答案, 不用可惜啊 ---- MEX 函數的用途, 是用 C 語言加速 Matlab 函數 既然是函數, 就會有輸入, 輸出 這一篇針對輸出介面做說明 輸出, 顧名思義, 函數丟出的東西 Matlab語言允許函數有多個輸出, 例如 [a, b] = myfun(c, d, e) 這個例子裡有三個輸入, 兩個輸出 ---- 傳統上, C語言的 main() 的輸入變數的數目一開始無法確定 所以用了類似下面這種語法... int main(int argc, char *argv[]) int argc 告訴 main(), 有幾個參數丟進來 char *argv[] 則是指向一張參數表, programmer 可以透過 argv[] 存取每個參數 ---- 回過頭來看 mex function 的 prototype void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) nlhs -> Number of Left-Hand-Side (input) plhs -> Parameter of Left-Hand-Side (input) nrhs -> Number of Right-Hand-Side (output) prhs -> Parameter of Right-Hand-Side (output) 這種寫法, 是不是和上面的 main() 基本上一模一樣 藉由這種作法, 我們可以讓 MEX function 有多個輸出入數值 如此一來, 就可以對應到 Matlab 的指令 [a, b] = myfun(c, d, e) 在這個例子裡 nlhs = 2 plhs[0] = a plhs[1] = b nrhs = 3 prhs[0] = c prhs[1] = d prhs[2] = e ---- 最後來個範例, 這個範例程式不會理會輸入 (nrhs, prhs) 只會丟出一個 3x2x4 的 Matrix 這個程式會用 mxCreateNumericArray() 建造輸出的 Matrix 詳細的 API 使用可以看 help mxArray 是 Matlab 內部實作的 data type 裡面放了 Matrix 的 dimension, data-type, 還有 data data是以一維陣列的方式儲存, 對於 N-Dimension 存取時 要根據 N-dimension 每個維度, 計算在一維陣列的位置 這部分就有點 tricky 了, 因為 Matlab 內部實作是 Fortran 而不是 C 所以資料在排列的時, 是先排 column 才是 row 以 3x2 Matrix 來講: 0 3 1 4 2 5 (Fortran Style) 一般習慣的 C-style 是 0 1 2 3 4 5 #include "mex.h" #include <math.h> #include <stdio.h> // Program test for output // usage: // mex test1.c // a = test1 // note: type "a=test1" in Matlab Command Window void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int output_dimension[3] = {3, 2, 4}; double *out; int i, j, k; int counter; int dim_x = output_dimension[0]; int dim_y = output_dimension[1]; int dim_z = output_dimension[2]; plhs[0] = mxCreateNumericArray( 3, // 3-D matrix output_dimension, // specify dimension of each dimension mxDOUBLE_CLASS, // use DOUBLE data type mxCOMPLEX); // output is COMPLEX out = mxGetPr(plhs[0]); // get pointer of real-part data // mx: Matrix // Get: Get // P: Pointer // r: Real // The function has a counterpart: mxGetPi() counter = 0; for(k=0; k<4; k++) for(j=0; j<2; j++) for(i=0; i<3; i++) { out[i + j*dim_x + k*dim_x*dim_y] = counter; counter++; } } ---- Practice. 1. 修改上面程式, 用 C 建立一個 3 x 4 的 Matrix, 並讓內容為 1 2 3 4 5 6 2. 承(1), 在 Matlab 輸入 a = [1 2 3; 4 5 6] 輸入 a(1), a(2), a(3), a(4), a(5), a(6) 體驗一下, Fortran-style 和 C-style 的不同 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.128.237 ※ 編輯: lihgong 來自: 140.113.128.237 (05/17 01:49)