使用memcpy

时间:2015-07-13 21:14:27

标签: c++ matlab

我尝试将c ++连接到matlab以获得图形功能。例如

Engine *m_pEngine;
m_pEngine = engOpen("null"); //open matlab engine

const int arraysize = 1000;
const double degTorad = .0174;

double SinArray[arraysize];
double CosArray[arraysize];
double Degrees[arraysize];
for (int iii = 0; iii < arraysize; iii++)
{
Degrees[iii] = iii;//gets the degree
SinArray[iii] = sin(iii*degTorad);
CosArray[iii] = cos(iii*degTorad);

}
cout << "Example 1 or 2?"<<"\n";
char Sim = '1';
cin >> Sim;

// example 1 below
// we will pass an entire array to be plotted

if (Sim == '1')
{
mxArray* SIN = mxCreateDoubleMatrix(arraysize, 1, mxREAL);
memcpy((void *)mxGetPr(SIN), (void *)SinArray, sizeof(double)*arraysize);
engPutVariable(m_pEngine, "SinGraph", SIN);

mxArray* COS = mxCreateDoubleMatrix(arraysize, 1, mxREAL);
memcpy((void *)mxGetPr(COS), (void *)CosArray, sizeof(double)*arraysize);
engPutVariable(m_pEngine, "CosGraph", COS);

mxArray* DEG = mxCreateDoubleMatrix(arraysize, 1, mxREAL);
memcpy((void *)mxGetPr(DEG), (void *)Degrees, sizeof(double)*arraysize);
engPutVariable(m_pEngine, "Degrees", DEG);

engEvalString(m_pEngine, "figure('units','normalized','outerposition',(0 0 1 1))"); //opens up matlab figure window
engEvalString(m_pEngine, "plot(Degrees,SinGraph,'r',Degrees,CosGraph,'b'),grid minor,title('our Matlab Plot')");

system("pause");
}

运行良好,我可以得到正确的结果。这意味着我的计算机中的环境设置是正确的。但是在运行下面的代码时:

for (int i = 0; i < 141; i++)    
    {
        x.push_back(((-141 / 2 + 1 - 1+i)*0.0003));
        y.push_back(((-141 / 2 + 1 - 1+i)*0.0003));
    }

engine *m_pEngine;
m_pEngine = engOpen("null"); //open matlab engine

mxArray *xxx = mxCreateDoubleMatrix(1, 141, mxREAL);
memcpy((void*)mxGetM(xxx), &x, sizeof(double)*x.size());
engPutVariable(m_pEngine, "x", xxx);


mxArray *yyy = mxCreateDoubleMatrix(1, 141, mxREAL);
memcpy((void*)mxGetM(yyy), &y, sizeof(double)*y.size());
engPutVariable(m_pEngine, "y", yyy);

它中断:PlanarforwardC.exe中的0x00007FFFB193AA69(msvcr120d.dll)处的未处理异常:0xC0000005:访问冲突写入位置0x0000000000000001。 它还说: &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ libmwcholmod.dll&#39;。无法找到或打开PDB文件。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ graphics_util.dll&#39;。无法找到或打开PDB文件。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ libmwplatform_res.dll&#39;。无法找到或打开PDB文件。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Windows \ System32 \ msimg32.dll&#39;。符号已加载。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ libmwamd.dll&#39;。无法找到或打开PDB文件。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ libmwcolamd.dll&#39;。无法找到或打开PDB文件。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ libmwblas.dll&#39;。无法找到或打开PDB文件。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ libmwlapack.dll&#39;。无法找到或打开PDB文件。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Windows \ System32 \ SHCore.dll&#39;。符号已加载。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ libmwbinder.dll&#39;。无法找到或打开PDB文件。 &#39; PlanarforwardC.exe&#39; (Win32):已加载&#39; C:\ Program Files \ MATLAB \ R2012b \ bin \ win64 \ libmwompwrapper.dll&#39;。无法找到或打开PDB文件。 DBGHELP:符号搜索路径:C:\ Users \ Gabrielle \ Desktop \ PlanarforwardC \ x64 \ Debug DBGHELP:C:\ Users \ Gabrielle \ Desktop \ PlanarforwardC \ x64 \ Debug \ m_interpreter.pdb - 找不到文件 DBGHELP:C:\ Users \ Gabrielle \ Desktop \ PlanarforwardC \ x64 \ Debug \ dll \ m_interpreter.pdb - 找不到文件 DBGHELP:C:\ Users \ Gabrielle \ Desktop \ PlanarforwardC \ x64 \ Debug \ symbols \ dll \ m_interpreter.pdb - 找不到文件 DBGHELP:y:\ R2012bd \ build \ matlab \ bin \ win64 \ m_interpreter.pdb - 找不到文件 DBGHELP:m_interpreter - 导出符号 DBGHELP:C:\ Users \ Gabrielle \ Desktop \ PlanarforwardC \ x64 \ Debug \ msvcr90.amd64.pdb - 找不到文件 DBGHELP:C:\ Users \ Gabrielle \ Desktop \ PlanarforwardC \ x64 \ Debug \ dll \ msvcr90.amd64.pdb - 找不到文件 DBGHELP:C:\ Users \ Gabrielle \ Desktop \ PlanarforwardC \ x64 \ Debug \ symbols \ dll \ msvcr90.amd64.pdb - 找不到文件 DBGHELP:msvcr90.amd64.pdb - 找不到文件

memcpy((void*)mxGetM(xxx), &x, sizeof(xx));

我也试图找到解决方案并使用double xx [141]和double yy [141]替换向量x和向量y。

mxArray *xxx = mxCreateDoubleMatrix(1, num_x, mxREAL);
memcpy((void*)mxGetM(xxx), (void*)xx, sizeof(xx));
engPutVariable(m_pEngine, "x", xxx);


mxArray *yyy = mxCreateDoubleMatrix(1, num_x, mxREAL);
memcpy((void*)mxGetM(yyy), (void*)yy, sizeof(double)*y.size());
engPutVariable(m_pEngine, "y", yyy);
它仍然没有奏效。 任何人都可以给我一些建议吗?我逐行测试,问题出在:

非常感谢

1 个答案:

答案 0 :(得分:0)

mxGetM返回数组的大小。此大小用作memcpy中的指针,这肯定会导致无效的内存地址。