coder.ceval for printf from stdio.h

7 views (last 30 days)
Hello, everybody! I'd like to generate C code from an m-file which makes use of MATLAB fprintf function as follows:
fprintf('Time level %-*.*f [s], Iterations: %d\n',field_width,precision,time,iter);
Since fprintf is not supported for the code generation, nor can I use coder.extrinsic method, I turn to a question:
How to rewrite the command using coder.ceval method and the standard C library function printf? To be more specific, what should the arguments be like in coder.ceval('printf', arguments); ?
While not using field_width and precision, C code generated from the following one executes without a problem:
s1=['%s%f%s%d' 10 0];
s2=['Time level ' 0];
s3=['[s], Iterations: ' 0];
However, including the asterisks and left-justify options cause segmentation fault whenever the generated C code is compiled, built and run. Any help is appreciated!
Tomas Jurena
Tomas Jurena on 30 May 2012
OK, no need to do that :) See the answer given by Fred Smith.

Sign in to comment.

Accepted Answer

Fred Smith
Fred Smith on 30 May 2012
This example worked for me:
function simple_printf
s1=['%s%-*.*f%s%d' 10 0];
s2=['Time level ' 0];
s3=['[s], Iterations: ' 0];
iter = 32;
time = 2.5;
Is your code different?
Kevin Berndsen
Kevin Berndsen on 18 Mar 2015
Great workaround for the string termination problem. Thanks!

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by