[英]MATLAB MEX-file builds successfully but nothing is displayed
我是這里的新手,所以我將嘗試清楚地揭示我的問題:我目前正在用MATLAB語言開發一個程序,該程序必須加載庫才能正常工作。 但是,后者是用C / C ++編寫的(我無法訪問它),但是我可以用C ++調用它,然后創建一個MEX文件以使用返回的值。 使用Visual Studio 2012,我成功地成功調用了該庫(當我將parameter-values用作參數時,portRead函數會返回一個值)。 這是我的代碼:
// Test704.cpp : Defines the entry point for the console application.
#define _AFXDLL
#define _tprintf mexPrintf
//#include "afx.h"
#include "StdAfx.h"
#include "704IO.h"
#include "Test704.h"
//#include "mat.h"
#include "mex.h"
//mxArray *matGetNextVariable(MATFile *mfp, const char **name);
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////
CWinApp theApp; // The one and only application object
/////////////////////////////////////////////////////////////////////////////
using namespace std;
/////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
//void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//int nRetCode(0);
HMODULE hModule(::GetModuleHandle(NULL));
short valueRead;
if (hModule != NULL)
{
// Initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
//_tprintf(_T("Fatal Error: MFC initialization failed\n"));
//mexPrintf("Fatal Error: MFC initialization failed");
//nRetCode = 1;
}
else
{
while(true)
{
valueRead = PortRead(1, 780, -1);
_tprintf(_T("Value Read = %i\n"), valueRead);
//mexPrintf("Value Read = %i",valueRead);
Sleep(1000); // Sleep for 1s so we can see the value on the screen
}
}
}
else
{
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
//mexPrintf("Fatal Error: GetModuleHandle failed");
// nRetCode = 1;
}
//return nRetCode;
}
/////////////////////////////////////////////////////////////////////////////
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int _tmain();
//short valueRead;
//valueRead = PortRead(1, 780, -1);
//_tprintf(_T("Value Read = %i\n"), valueRead);
//mexPrintf("Value Read = %i",valueRead);
return;
}
您可以看到我評論了我為解決問題而進行的不成功的研究……此代碼在控制台中返回“ valueRead = 255”,這意味着程序運行良好。 我現在想要的是在MATLAB中檢索此值。 您可能還會注意到,我創建了一個mexFunction; 確實,我讀到文檔中說必須使用C ++創建通往MATLAB的網關(= mexFunction)。
現在,使用MATLAB R2015a,我創建了以下MEX文件:
function test()
location = 'C:\Users\admin\Documents\MATLAB\';
mylib = [location '704IO.lib'];
mex( 'Test704.cpp', mylib)
而不是檢索“ valueRead”,我只收到一條消息,指出
>Building with 'Microsoft Visual C++ 2012'. MEX completed successfully
因此,作為總結,我有功能性的C ++代碼,但無法弄清楚為什么無法將其鏈接到MATLAB以使用它! 我已經在尋找解決方案好幾天了,我認為現在是時候尋求幫助了:)
非常感謝你的幫助! (P.-S .:我是C ++的初學者,對語法理解錯誤/錯誤表示抱歉)
通過致電
mex( 'Test704.cpp', mylib)
您只編譯 .cpp
文件。 結果,MATLAB告訴您MEX completed successfully
,這意味着編譯有效。 結果,您將在工作目錄中看到一個新文件:根據您的操作系統,它可能被稱為Test704.mexw64
(Windows,64位)或類似的名稱。 每當您在C ++代碼中進行了某些更改時,都需要調用mex
,以便重新編譯該文件。
現在,可以像普通的MATLAB函數一樣使用此編譯的文件/函數。 要調用它而沒有任何其他參數,只需鍵入
Test704()
在MATLAB中。 這樣,將執行mexFunction()
,並以您的_tmain()
運行。
注意:您的C ++代碼會將例如valueRead = 255
寫入MATLAB命令窗口,但不會在MATLAB中創建名為valueRead
的變量。 要創建這樣的變量,您必須將_tmain
的讀取值返回給mexFunction
,然后在其中分配一個MATLAB變量。
編輯:您在mexFunction
有一個錯誤:您在mexFunction
編寫:
int _tmain();
這不是函數調用,而是函數聲明 。 您告訴C++
,您有一個名為_tmain()
的函數,但從未調用過。 您需要的是一個函數調用,即只有_tmain()
而沒有int
。 請記住,您需要提供int argc, TCHAR *argv[], TCHAR *envp[]
。 我只是在這里將它們設置為零:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
_tmain(0,0,0);
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.