Sprintf not supported with c codegen. Which is the workaround?

10 vues (au cours des 30 derniers jours)
Oriol Rios
Oriol Rios le 4 Août 2016
SPRINTF is not a codegen supported function (although fprintf is). I tried to create a dedicated MEX but does not work. Does any one can figure out a workaround? Could not find any working example with coder.ceval neither
Thanks!

Réponse acceptée

Tom Clark
Tom Clark le 20 Sep 2016
The accepted answer on this SO post shows one way of doing it (for ints) which could be adapted to floats, presumably).
I think it's a really massive shortcoming that coder can't do such a basic thing!
  1 commentaire
Oriol Rios
Oriol Rios le 21 Sep 2016
Thanks Tom!
I kind of found a workaround by manually scripting a C functions with its gateway that does the job (particular to my problem). Your answer might be a solution too.
Many thanks!

Connectez-vous pour commenter.

Plus de réponses (3)

Nathan Zimmerman
Nathan Zimmerman le 20 Juin 2017
How on earth has matlab been around for so long with all these code generation tools but converting a number to a string is not supported? Dear lord why? How many thousands of dollars do these code tools cost again?
  1 commentaire
Walter Roberson
Walter Roberson le 20 Juin 2017
num2str() or sprintf() to string has a variable length result. That is a problem when generating for embedded hardware with limited memory, as embedded hardware for real-time work usually requires fixed length buffers. The C language function sprintf() does not accept a buffer size, so it tends to lead to buffer overflow problems, including deliberate security attacks. (Remember, although you can give precision modifiers to sprintf, they are always minima and sprintf will use more characters if it needs to.)
You really need C snprintf() or vsnprintf() calls, but those came out in C99 and were not present in C89.
One thing to keep in mind in this respect is that Microsoft has said clearly that Visual Studio will not be made C99 compliant, because Visual Studio is intended to be primarily a C++ compiler, so Microsoft only implemented the parts of C99 that were directly compatible with C++99 (or later updates, depending on the VS edition you get.)
It appears to me that snprintf() does exist in C++ so it is probably supported by VS in C mode -- the point here is that limitations like these can be a lot more subtle than is at first apparent.

Connectez-vous pour commenter.


Azzi Abdelmalek
Azzi Abdelmalek le 4 Août 2016
You can use num2str
  1 commentaire
Walter Roberson
Walter Roberson le 4 Août 2016
No, num2str is not supported for code generation either.

Connectez-vous pour commenter.


Raghu Boggavarapu
Raghu Boggavarapu le 10 Jan 2022
Modifié(e) : Raghu Boggavarapu le 10 Jan 2022
As of MATLAB R2018a sprintf is supported for MATLAB Coder code generation.
  2 commentaires
Raghu Boggavarapu
Raghu Boggavarapu le 10 Jan 2022
I stand corrected. Updated the comment now.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by