Matlab Coder, C-function return type

조회 수: 7 (최근 30일)
Christian Steinebach
Christian Steinebach 2017년 10월 16일
댓글: Christian Steinebach 2019년 6월 26일
What determines the return of a parameter when generating C-code from a function? In which case is the parameter returned by value (as the c-function's value) and in which case is it returned in the parameter list? How can I ensure that the generated c-function is alwasy of type void()? Thanks for helping
  댓글 수: 1
Christian Steinebach
Christian Steinebach 2017년 10월 19일
Hei again!
Nobody can help? :-( There must be some rules how the code generator decides whether to return anything as a parameter or as the function's return value. code.typeof() is only for input values as far as I understood. Example:
matlab function res=doit(a, b)
I would prefer a corresponding c-function void doit(double a, double b, double *res) eventually void doit(double a, double b, double res[])
Instead I get double doit(double a, double b)
Do I define another dummy parameter in the matlab function definition like function [res, dummy]=doit(a, b)
I get the generated c-code void doit(double a, double b, double *res, double *dummy)
That's almost what I want. But I have no use for the dummy variable.
Any suggestions?
Chris

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

채택된 답변

Abhishek Kumar
Abhishek Kumar 2019년 6월 26일
Hi Christian
Functions in programming languages like 'c' and 'c++' are of a certain type, whether it be int, double, string etc. And when called using the main program, these functions return a value of that type. That is not the case in MATLAB, here we don't define return type also the function can return many values.
The coder is designed in such a way that when the matlab function returns more than one value the value to be returned is passed as parameters in the function.
If you have only one return and want is passed as parameter in the function, you could do this:
..............................................
function res = doit(a, b, res)
res= a+b;
end
................................................
>>codegen doit -args {0, 0, 0} -config:lib -report
  댓글 수: 1
Christian Steinebach
Christian Steinebach 2019년 6월 26일
Thank you, Abhishek Kumar ! :-)
That would have solved my problem. I sticked to the solution to check the number of output variables and react on that.
Thank you for your answer, I'll have it in mind next time.
Regards
Christian

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

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by