如果代码分离,MATLAB 引擎 API 将永远运行

MATLAB Engine API runs forever if code is separated

我正在尝试使用 MetaTrader 终端 4 通过引擎访问 MATLAB
(MetaQuotes 的交易软件)

我使用 DLL 文件在 MetaTrader 和 MATLAB 之间进行通信。

在代码中,如果我将这段代码放在一个函数中,它就可以工作。

但是如果我将它分成两个不同的函数,它会运行 forever/bugs out

#include "stdafx.h"

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <windows.h>
#include <memory.h>

#include "engine.h"

using namespace std;

#define MT4_EXPFUNC __declspec(dllexport)

Engine  *pEng      = NULL;
mxArray *closev    = NULL;
double  *closevp   = NULL;
mxArray *getPArray = NULL;

MT4_EXPFUNC void InitEngine( int dummy )
{
    pEng = engOpen( NULL );
}

MT4_EXPFUNC void InitCloseBuffer( int size )
{
    closev = mxCreateDoubleMatrix( 1, size, mxREAL );
    if ( closev != NULL )
         closevp = mxGetPr( closev );

 // ------------------------------------------------------------------
 // the following code bugs when separated from the code above
 // and put in another function called right after this one

    engPutVariable( pEng, "closev",  closev );
    engEvalString(  pEng, "[mainNet] = PTrainInit();" );
    engEvalString(  pEng, "[hitrate, mainNet] = PTrain(mainNet, closev);" );

    engEvalString(  pEng, "outGetP = PGetPrediction(mainNet, closev)" );

    getPArray = engGetVariable( pEng, "outGetP" );

    double *p;
    if ( getPArray != NULL )
        p = mxGetPr( getPArray );

// end of the separated code
// -----------------------------------------------------------------
}

不过我需要将两个函数分开,
因为我需要用值填充 closev

我做错了什么?

谢谢

杰夫

好的,我找到了答案:最初我做了一个独立的 MATLAB 项目,它运行良好,但 closev 是按列排列的,在 DLL closev 是按行的。

此外,填充 closev 然后使用第二个分离的函数与未填充 closev 时完全不同。

这两件事让它在错误方面看起来很奇怪。

很高兴我能成功。

JfLB