Matlab Coder, C-function return type

8 visualizaciones (últimos 30 días)
Christian Steinebach
Christian Steinebach el 16 de Oct. de 2017
Comentada: Christian Steinebach el 26 de Jun. de 2019
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 comentario
Christian Steinebach
Christian Steinebach el 19 de Oct. de 2017
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

Iniciar sesión para comentar.

Respuesta aceptada

Abhishek Kumar
Abhishek Kumar el 26 de Jun. de 2019
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 comentario
Christian Steinebach
Christian Steinebach el 26 de Jun. de 2019
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by