【问题标题】:Transfer a variable from .cpp file to Matlab workspace after simulation process仿真过程后​​将变量从 .cpp 文件传输到 Matlab 工作区
【发布时间】:2016-02-04 08:22:24
【问题描述】:

我目前在带有 R2015b 的 Matlab 中使用名为 Policy Search 工具箱的工具箱。我“混合”了所有文件,工具箱工作得很好。 计算微分方程的 .cpp 文件之一,在 Matlab 中的一个函数中计算并使用一个值。 由于工具箱与数据管理器一起使用,我不能只在模拟过程之后调用变量。 由于该函数是工具箱的巨大(!)构造的一部分,因此我不能只修改输出或复制粘贴并在其他地方调用该函数。

#include "mex.h"
#include <math.h>


void mexFunction(int nlhs, mxArray *plhs[], 
             int nrhs, const mxArray *prhs[])
{
     // Input
double
*startPosition      = mxGetPr(prhs[0]),

.......// some more variables ...

       // Output
plhs[0] = mxCreateDoubleMatrix(numJoints, numTrajectorySteps, mxREAL);
plhs[1] = mxCreateDoubleMatrix(numJoints, numTrajectorySteps, mxREAL);
plhs[2] = mxCreateDoubleMatrix(numJoints, numTrajectorySteps, mxREAL);

double
*Y = mxGetPr(plhs[0]),
*Yd = mxGetPr(plhs[1]),
*Ydd = mxGetPr(plhs[2]);

 .......// some more code ...

        double smoothedForcingFunction = iTrajectoryStep < numForcedSteps ?       forcingFunction[oldI] : 0;
        double Ydd = (alpha_x*(beta_x*(goalTemp-Y[oldI])+(goalVel-Yd[oldI])/ tau)  +(amplitude[iJoint]*smoothedForcingFunction))*tau * tau;

        // simplectic Euler
        Yd[curI] = Yd[oldI] + dt*Ydd;
        Y[curI] = Y[oldI] + dt*Yd[curI];

    }        
}
//    printf("Done\n");
    mxDestroyArray(xData);
    mxDestroyArray(xdData);
    mxDestroyArray(tData);

}

我怎样才能将YDD(底部的计算值)从那里取出到我的 Matlab 工作区中?是否有一种“公共 goto 工作区作为输出 plz!” C++ 中的函数?

非常感谢您的帮助!

【问题讨论】:

  • Ydd的大小和数据类型是多少?

标签: c++ matlab mex


【解决方案1】:

如果Ydddouble * 数组,大小为nYdd(您需要知道),那么您可以使用指针plhs 将其分配给输出。

代码如下:

//Note that this is for output n1. If you want to output more things use plhs[1], plhs[2],...

// Allocate memory for the output:
// It is 1 dimensional, with nYdd elements, double type and real numbers only
plhs[0] = mxCreateNumericArray(1,nYdd, mxDOUBLE_CLASS, mxREAL);
// Get the pointer into your own variable
double *mxYdd =(double*) mxGetPr(plhs[0]);
// copy whateer is in Ydd into mxYdd
memcpy(mxYdd ,Ydd,nYdd*sizeof(double));
// delete Ydd (its already copyed
free(Ydd);

我猜你可以写一个函数为

void public_goto_workspace_as_output_plz(mxArray* plhs,double* Ydd, size_t nYdd);

上面有这个,但可能不需要;)

文档:

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 2016-10-20
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 2011-02-15
    相关资源
    最近更新 更多