MATLAB Answers

Matlab Coder, C-function return type

30 ビュー (過去 30 日間)
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?



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;
>>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.


その他の回答 (0 件)


Community Treasure Hunt

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

Start Hunting!

Translated by