MATLAB Answers

How to reference mxArray between sFunctions with help of pWork?

2 views (last 30 days)
Christian on 22 Oct 2018
Edited: Christian on 23 Oct 2018
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)
mxArray *work_vector = NULL; // mxCalloc(1, sizeof(mxArray*));
work_vector = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
if(work_vector == NULL)
{ ssSetErrorStatus(S, "Faild to alloc memory for pWork Vector.\n"); }
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)
// 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
// Update _tic
mexCallMATLAB(1, &tic, 0, NULL, "tic");


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

Answers (0)

Sign in to answer this question.