Error calling char variable from C code by function block of Simulink

조회 수: 3 (최근 30일)
Alberto Prudhomme
Alberto Prudhomme 2016년 4월 18일
답변: Mark McBroom 2016년 5월 8일
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:
I hope you can help me to understand the problem,
Thanks for your time
Regards
Alberto

답변 (2개)

Venkatachala Sarma
Venkatachala Sarma 2016년 5월 6일
Hi Alberto,
'coder.ceval' should be used only for code generation. For simulation, you will have write your own algorithm.
One such example is provided in the documentation here

Mark McBroom
Mark McBroom 2016년 5월 8일
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];

Community Treasure Hunt

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

Start Hunting!

Translated by