Matlab Coder : Problem with string
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I try to put Trace on my model
I call the tracing function of an external library.
In my model : I do
T_Trace('This is my trace');
In the T_Trace function I do :
function T_Trace( Texte ) %#codegen
if isempty(coder.target)
% Executing in MATLAB, call MATLAB equivalent of
% C function foo
calllib('MATLAB_TOOLS','Tracing',Texte);
else
% Executing in Embedded MATLAB, call C function foo
%
coder.ceval('Tracing',Texte);
end
end
When I generate a C++ code, Matlab coder create :
real_T tracing_c(real_T A) {
static const char_T Texte[16] = { 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'm',
'y', ' ', 't', 'r', 'a', 'c', 'e' };
char_T b_Texte[16];
for (i0 = 0; i0 < 16; i0++) {
b_Texte[i0] = Texte[i0];
}
Tracing(b_Texte);
...
Why Matlab Coder create a "for" for make a copy of Texte into b_Texte ? Why Matlab coder make a copy ? The problem is when i make a lot of trace, i have a lot of "for" and i can't compil the C code because the number of for is too high ... How can i ask to matlab coder to not make a copy of all string ??
Thank you !
1 commentaire
Kaustubha Govind
le 28 Nov 2011
Michel: I'm not sure if there is a way to avoid this type of generated code. I would recommend reporting this to MathWorks Tech Support.
Réponses (2)
Walter Roberson
le 28 Nov 2011
Coder must make a copy of the string if it cannot prove that the called function does not modify the string. Usually compilers are permitted (by the language standards) to write strings in to read-only-memory, and Coder does not know whether the eventual target compiler will have that property or not.
0 commentaires
Mike Hosea
le 28 Nov 2011
You are passing texte as a read+write argument. If your function does not modify the argument, try coder.rref():
coder.ceval('Tracing',coder.rref(Texte));
Also, be careful when passing a string from MATLAB to C. In C strings are null-terminated, but in MATLAB strings are not null-terminated (their length is implicitly carried around with them). -- Mike
0 commentaires
Voir également
Catégories
En savoir plus sur MATLAB Coder dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!