S-Function return types

조회 수: 3 (최근 30일)
Daniel
Daniel 2012년 8월 16일
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
José-Luis
José-Luis 2012년 8월 16일
편집: José-Luis 2012년 8월 16일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Kaustubha Govind
Kaustubha Govind 2012년 8월 16일
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
Daniel
Daniel 2012년 8월 16일
Thanks!
All was of great help!
Kaustubha Govind
Kaustubha Govind 2012년 8월 17일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by