S-Function return types

Hello,
I am trying to generate an S-Function code from a C++ source code in which I wish both input and output to be arrays.
In my C++ code the function follows:
double* function(double param1[], param2[])
{
double *varReturn = new double[sizeof(param1[])];
...
//body
...
return varReturn;
}
In the Matlab environment I enter the Output definition:
def.OutputFcnSpec = 'double y1 = TPh(double u1[], double u2[])'; then I get the
error:
error C2440: '=' : cannot convert from 'double *' to 'real_T',
logically due to "double y1" does not be an array nor a pointer declaration. However I am having a hard time in write the output definition to make it work.
Can someone please give me some help with this. I sense I am being careless at some details or I have a misunderstanding.
Thank you.
Sincerely,
Dan

1 Comment

José-Luis
José-Luis on 16 Aug 2012
Edited: José-Luis on 16 Aug 2012
in C++, if you have a pointer to some data and then you set the value of the data inside the function, you can access it outside the function. If you define the pointer inside the function, as you seem to be doing, then the return values will be undefined once the function exits. This might be the source of the problem.

Sign in to comment.

 Accepted Answer

The compiler error occurs, because in your specification:
double y1 = TPh(double u1[], double u2[])
The output 'y1' is returned by value, not a pointer which your function returns. As José-Luis Guerrero suggested, you should probably return the output via an input pointer argument, so that your specification can be:
TPh(double u1[], double u2[], double y1[])

9 Comments

Daniel
Daniel on 16 Aug 2012
Thank you both. Progress has been made with your suggestions.
Although I need an S-function block in simulink that will have two array inputs and one array output.
If I define a function as: function(double u1[], double u2[], double y1[])
I have no output port generated! Additionally I need to specify the size of the output array for example: .... double y1[10])
I want to have a dynamically sized output array that is equal to the return array from my C++ function.
How is it possible to this?
Thank you!
Daniel
Daniel on 16 Aug 2012
Actually I must correct what I said before. I do have an output port generated!
The only thing needed to be solved is the output size. Can I make it dynamically?
Thank you.
José-Luis
José-Luis on 16 Aug 2012
Edited: José-Luis on 16 Aug 2012
I am not a simulink user. But if you need dynamically allocated memory, and want to avoid the hassle of using new, is it not possible to use an stl container (e.g. a vector)? If you have to pass a pointer, and know what the size is in advance, then put your pointer declaration outside your function:
double *y1 = new double[mySize];
void function(double* param1, double* param2,double* y1)
{
y1[0] = whatever;
y1[1] = whatever;
//provided the index is valid
}
Daniel
Daniel on 16 Aug 2012
Hello Jose,
I do not know in advance the size of the output vector, I know it is the same size of the input vectors though:
function(double u1[], double u2[], double y1[size of u1 or u2])
Being u1[] and u2[] DYNAMICALLY SIZED.
Would something like that be possible?
Thank you.
José-Luis
José-Luis on 16 Aug 2012
Edited: José-Luis on 16 Aug 2012
If you know the size of u1 and u2 beforehand just allocate space for your output before you run your function. double u1[] is a pointer, it gives no indication of the size. The space allocated for it should be somewhere in your code. If you find it then you can allocate y1 as indicated in the previous comment.
Cheers!
Daniel
Daniel on 16 Aug 2012
Thank you for keeping answering me back! I don't know the input array sizes, it varies on a case-by-case basis, that is why their size is allocated dynamically. The only thing I know is that the output array size will always be the same size as the inputs.
Thank you.
José-Luis
José-Luis on 16 Aug 2012
Edited: José-Luis on 16 Aug 2012
No worries. It is not possible to get the size of dynamically allocated memory from a pointer:
http://stackoverflow.com/questions/3693651/how-to-get-the-length-of-dynamically-allocated-two-dimensional-arrays-in-c
You need to know the size beforehand if you want to allocate it. In plain C, I would allocate as big a chunk of memory as i dared, then check that you don't get out of bounds, and resize upon exit. In C++ i would use a vector and pass that as an argument.
This is not really a Matlab question anymore. Cheers!
Daniel
Daniel on 16 Aug 2012
Thanks!
All was of great help!
Daniel: Simulink already allocates the memory for the output, so you shouldn't be allocating it again. In fact, you might see memory leaks and other bad things happen if you overwrite the pointer (y1) allocated by Simulink to a different location.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!