精華區beta MATLAB 關於我們 聯絡資訊
上一篇在講怎麼處理 output 這一篇我們來看看怎麼處理 input 圍繞著整個主題, C-style 和 Fortran-style 之間的轉換 是看懂這些程式的關鍵 outline * 辨識有多少個輸入變數 * 讀取輸入變數的內容 ---- 直接從一個 example 開始吧 #include "mex.h" #include <math.h> #include <stdio.h> void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { printf("%d\n", nrhs); } 把檔案取名為 test_input.c 編譯 mex test_input.c 並且執行以下指令, 觀察結果 test_input(1) test_input(1, 2, 3) test_input(1, 2, 3, 4, 6) 這個做完以後, 應該可以看出 nrhs 這個變數的意義 先用這個簡單的例子當做開場 ---- 接下來, 設計一組簡單的輸入... input = zeros(2, 5) input(1, :) = 1:5 input(2, :) = 5:-1:1 如此 input = [ 1 2 3 4 5 5 4 3 2 1] 我想設計一個 MEX-function 讀取並印出第一個 row, 其次是第二個 row 注意在下面的程式裡 mxGetM() 取得 Matrix 有幾個 row mxGetN() 取得 Matrix 有幾個 column 程式不困難, 有任何問題打 helpwin 配上 search 函數名稱打進去都看得到使用說明 ---- #include "mex.h" #include <math.h> #include <stdio.h> // Program test for input // usage: // input = zeros(2, 10) // input(1, :) = 1:10 // input(2, :) = 10:-1:1 // mex test2.c // test2(input) // note: type all commands above in Matlab Command Window void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int i, j, k; int input_dim_x; int input_dim_y; double *in; // pointer to process content of the input double *test; // print some message about input data printf("nrhs: %d\n", nrhs); printf("mxGetM(prhs[0]): %d\n", mxGetM(prhs[0])); printf("mxGetN(prhs[0]): %d\n", mxGetN(prhs[0])); in = mxGetPr(prhs[0]); // get data pointer input_dim_x = mxGetM(prhs[0]); input_dim_y = mxGetN(prhs[0]); // print input content for(i=0; i<input_dim_x; i++) // x for(j=0; j<input_dim_y; j++) // y // notice: data type is "float", you shall use "%f" insted of "%d" printf("%f\n", in[i + j*input_dim_x]); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.128.237