MATLAB Answers

0

How to reference mxArray between sFunctions with help of pWork?

Asked by Christian on 22 Oct 2018
Latest activity Edited by Christian on 23 Oct 2018
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 Comments

Is there some reason you don't want to use the C/C++ clock( ) function for this timing?
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.

Sign in to comment.

0 Answers