C-mex function output entire array

1 view (last 30 days)
Alex
Alex on 11 Sep 2013
Hello,
The outputs of a S function are usually defined as follows:
static void mdlOutputs(SimStruct *S, int_T tid)
{
int_T i;
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T *y = ssGetOutputPortRealSignal(S,0);
int_T width = ssGetOutputPortWidth(S,0);
for (i=0; i<width; i++) {
*y++ = 2.0 *(*uPtrs[i]);
}
}
That is to say that for each timestep, an output value is calculated. For instance, we would have y.time the time values of the simulation and y.signals.values the values corresponding to each time value.
But I would like to do something different. Like:
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *y = ssGetOutputPortRealSignal(S,0);
int Number=15;
double* var = (double*)malloc(Nptsfreq*sizeof(double)); // array of variable size
// Calculate the content of var
// Output the entire array var at each time step
}
Currently I am only able to assign ONE value of the array var to the ouput by doing *y=var[0] (here for instance the first value).
Moreover, the size of var varies at each new timestep. All I want is to output all the content of var using the output port (it is important that the information is contained in the output port since I need to use the function with ControlDesk). Is this possible?
Thank you :)

Answers (4)

Kaustubha Govind
Kaustubha Govind on 11 Sep 2013
Signals that change size at run-time are called variable-size signals in Simulink. Please read Variable-Size Signal Basics to understand more about them. You may want to look at C S-Function with Variable-Size Signals for an example of how to output a variable-size signal from your S-function.

Alex
Alex on 12 Sep 2013
Thank you for your answer. However, in my case the signal doesn't only change at run-time, but I need that for each call of the function mdlOutputs an entire vector is sent to the output. Or I only can only export one value by doing
real_T *y = ssGetOutputPortRealSignal(S,0);
*y = value_exported;
An entire array should be exported for each integration time (and the number of elements of this array varies also).
  1 Comment
Kaustubha Govind
Kaustubha Govind on 19 Sep 2013
Alex: Please post follow-up questions as comments, rather than as answers.
From what I understand, variable-size signals is exactly what you need (not sure why you disagree). You can set the output port width at each time-step using ssSetCurrentOutputPortDimensions as in the example that I linked to.

Sign in to comment.


Alex
Alex on 12 Sep 2013
By doing
ssSetOutputPortVectorDimension(S, 0, 1000);
I can output a vector of size 1000 at each time step. However, when the size of the vector I want to output is lower than 1000 the extra values are replaced with zeros. I don't want to do that.
I don't find out how to set the dimension of the outpot port vector dynamically in mdlOutputs (where the information about its length is available).

Alex
Alex on 19 Sep 2013
I am still looking for a solution, if someone has any idea :)
  1 Comment
A Jenkins
A Jenkins on 19 Sep 2013
Edited: A Jenkins on 20 Sep 2013
The code Kaustubha Govind linked to above shows an example of how to use
ssSetOutputPortDimensionInfo(S, 0, DYNAMIC_DIMENSION)
Please tell us if you have run the example and why it does not work for you?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!