[英]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.