Error calling char variable from C code by function block of Simulink
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello all again;
I want to ask for your help to understand this problem. I need to make a converter from float value to string, to be send by serial transmit of my Tiva C launchpad. Im trying to make the conversion in a simulink function block using the sprintf function.
During the debug in the Code Compiler Studio it works fine, I only receive a warning that the return variable are not compatible with the format, but the value gets saved to the char variable.
The problem ocurr with Matlab, because regardless the value of the constant, the block always give me the value of -128; I was searching for hours, but i can't found any solution.
This is the C code:
#include <stdio.h>
#include "dobloh.h"
double u;
char doble(double u)
{
static char car[2000];
sprintf(car,"%.3f",u);
return car;
}
"dobloh.h" Header:
#ifndef DOBLADOR_C_
#define DOBLADOR_C_
char doble(double u);
#endif /* DOBLADOR_C_ */
Function block code:
function y = callingDoblez(u)
%#codegen
y=0.0;
y = coder.ceval('doble',u);
Diagram:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/161656/image.png)
I hope you can help me to understand the problem,
Thanks for your time
Regards
Alberto
0 commentaires
Réponses (2)
Venkatachala Sarma
le 6 Mai 2016
Hi Alberto,
'coder.ceval' should be used only for code generation. For simulation, you will have write your own algorithm.
0 commentaires
Mark McBroom
le 8 Mai 2016
Coder.feval can be used for simulation and code generation. The problem is that your function is supposed to return a single char, but in the c function you are returning a vector, which means the address of car is getting returned. That is likely the cause of the compiler warning. If you only want the first char to be returned, change your code to:
return car[0];
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!