将值从C ++ MEX文件返回到MATLAB [英] Return values from a C++ MEX file to MATLAB

查看:204
本文介绍了将值从C ++ MEX文件返回到MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个从C ++代码中检索数据的MATLAB程序。为此,我在MATLAB和网关mexFunction创建了一个MEX文件。
虽然读取的值可以在MATLAB中读取,但我无法检索它来使用它。如果这不清楚,我有与这里相同的问题(这是我的C ++代码:

I am coding a MATLAB program that retrieves data from C++ code. To do so, I have created a MEX-file in MATLAB and a gateway mexFunction. Although the read value can be read in MATLAB, I cannot retrieve it to work with it. If this is unclear, I have quite the same problem as here (How to return a float value from a mex function, and how to retrieve it from m-file?) but I want to retrieve a value displayed by my C++ program. Here is my C++ code:

#define _AFXDLL
#define  _tprintf mexPrintf
#include "StdAfx.h"
#include "704IO.h"
#include "Test704.h"
#include "mex.h"
#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[])
{
HMODULE hModule(::GetModuleHandle(NULL));
short   valueRead;

  if (hModule != NULL)
  {
    // Initialize MFC and print and error on failure
    if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
    {
      //mexPrintf("Fatal Error: MFC initialization failed");
      //nRetCode = 1;
    }
    else
    {
        valueRead = PortRead(1, 780, -1);
        mexPrintf("Value Read = %i\n",valueRead);
    }
  }
  else
  {
    _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
  }
  //return nRetCode;
}

/////////////////////////////////////////////////////////////////////////////
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    _tmain(0,0,0);
    return;
}

请告诉我,如果您需要我的问题的任何进一步的信息? >

Please tell me if you need any further information about my issue?

推荐答案

要将整数返回到Matlab,可以按照您发布的链接的说明进行修改, p>

To return an integer to Matlab you can follow the instructions of the link you posted but modifying it a bit so integers are returned.

void mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
    // Create a 1-by-1 real integer. 
    // Integer classes are mxINT32_CLASS, mxINT16_CLASS, mxINT8_CLASS
    // depending on what you want.
    plhs[0] = mxCreateNumericMatrix(1, 1, mxINT16_CLASS, mxREAL);


    // fill in plhs[0] to contain the same as whatever you want 
    // remember that you may want to change your variabel class here depending on your above flag to *short int* or something else.
    int* data = (int*) mxGetData(plhs[0]); 
    // This asigns data the same memory address as plhs[0]. the return value

    data[0]=_tmain(0,0,0); //or data[0]=anyFunctionThatReturnsInt();
    return;

}

这篇关于将值从C ++ MEX文件返回到MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆