How to reference mxArray between sFunctions with help of pWork?

2 次查看(过去 30 天)
Hello,
I'm trying to call 'tic' and 'toc' MATLAB functions from within a sFunction Level 2. Since I want to have a rough information about the time between two calls to mdlUpdate of a block, the retirnvalue of 'tic' needs to be availale during calls.
I thought of initializing 'tic' during mdlStart, calculating 'toc' during mdlUpdate and afterwards, also in mdlUpdate, updating 'tic'. Therefore I tried to use the pWork Vector, but somehow seem to be missing something.
Whenever I try to call themxCallMATLAB() function, I get an exception, declaring 'tic' needs to be of type uint64. Therefore I think something went wrong during the pointing the pWork Vetor at the allocated mxArray.
static void mdlStart(SimStruct *S)
{
if (mxGetScalar(ssGetSFcnParam(S, SS_PARAMETER_BLOCK_TYPE)) == SS_BLOCKTYPE_CONFIG)
{
mxArray *work_vector = NULL; // mxCalloc(1, sizeof(mxArray*));
work_vector = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
mexMakeArrayPersistent(work_vector);
if(work_vector == NULL)
{ ssSetErrorStatus(S, "Faild to alloc memory for pWork Vector.\n"); }
else
{
ssSetPWorkValue(S, SS_PWORK_CONF_TIC, work_vector);
if(S->work.pWork[0] != work_vector)
{ ssSetErrorStatus(S, "[CONFBLOCK] Failed to assign PWork vector.\n"); }
// Init 'tic' value
mexCallMATLAB(1, &work_vector, 0, NULL, "tic");
}
}
}
static void mdlUpdate(SimStruct *S, int_T tid)
{
if (mxGetScalar(ssGetSFcnParam(S, SS_PARAMETER_BLOCK_TYPE)) == SS_BLOCKTYPE_CONFIG)
{
// Block Information
mxArray *tic, *elapsed;
tic = (mxArray*) ssGetPWork(S);
// tic = (mxArray*) ssGetPWorkValue(S, , SS_PWORK_CONF_TIC);
elapsed = mxCreateDoubleMatrix(1,1, mxREAL);
mexCallMATLAB(1, &elapsed, 1, &tic, "toc");
// Evaluate 'toc' result
double *secs = mxGetDoubles(elapsed);
DEBUG_MSG("[ConfigBlock] TSM_Timer_Milliseconds (%u)", milliseconds);
// Call tsm_timer
tsm_timer_milliseconds(milliseconds);
// Update _tic
mexCallMATLAB(1, &tic, 0, NULL, "tic");
}
}
  2 个评论
James Tursa
James Tursa 2018-10-22
Is there some reason you don't want to use the C/C++ clock( ) function for this timing?
Christian
Christian 2018-10-23
That probably what I will do, but it was a first for me to call a MATLAB function with mexCallMATLAB(), so I was a bit stubborn in finishing this. Just out of curiosity.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by