2017-07-16 60 views
0

我開始與墨西哥MATLAB的合作,建立交流代碼和我寫了一個非常簡單的代碼(main.c中)開始說起:mexPrintf消息不MATLAB的命令窗口中顯示

#include "stdio.h" 
#include "stdlib.h" 
#include "mex.h" 

void main() 
{ 
    mexPrintf("Hello world"); 
} 

當我輸入mex main.c在matlab腳本中一切順利,我有這樣的信息:「用'gcc'構建.MEX成功完成。」但我沒有看到消息「Hello world」,我也嘗試過printf(),但沒有成功,有人知道爲什麼消息不會出現在matlab窗口上嗎?

在此先感謝您的幫助。

-J

+0

從您的問題聽起來您只是將代碼編譯爲mexfile。你執行了mexfile嗎? – user4581301

+0

感謝您編輯我的代碼@ user4581301。是的,我執行了代碼,但它沒有工作,實際上它需要一個mexFunction,它工作正常。 – mja

回答

1

您的代碼,因爲它的立場,不與Matlab的兼容。 Matlab需要編譯程序的'main'函數的特殊函數定義。

爲了運行你的代碼,你需要有這樣的事情:

#include "stdio.h" 
#include "stdlib.h" 
#include "mex.h" 

void mexFunction(int nlhs, mxArray *plhs[], 
      int nrhs, const mxArray*prhs[]) 
{ 
    mexPrintf("Hello world\n"); 
} 

假設這是擺在題爲「helloWorld.c」文件,你可以在運行以下命令Matlab提示:

mex helloWorld.c 
helloWorld 
+0

是的,你是絕對正確的@ acampb311,我的代碼需要一個mexFuction,當我添加它時,它工作完美,非常感謝你! – mja