我目前在Matlab中使用名为Policy Search toolbox的工具箱和R2015b。我“mexed”所有文件,工具箱工作得很好。 其中一个.cpp文件计算微分方程,计算一个值并在Matlab中的一个函数中使用。 由于工具箱正在使用数据管理器,因此我无法在模拟过程之后调用变量。 由于该函数是工具箱的giantic(!)结构的一部分,我不能只修改输出或复制和粘贴并在其他地方调用该函数。
#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 ++中的函数?
非常感谢你的帮助!
答案 0 :(得分:0)
如果Ydd
是double *
数组,大小为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);
有了这个,但可能不需要;)
文档: