coder.ceval for printf from stdio.h

조회 수: 5 (최근 30일)
Tomas Jurena
Tomas Jurena 2012년 5월 27일
댓글: Kevin Berndsen 2015년 3월 18일
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];
coder.ceval('printf',s1,s2,time,s3,int32(iter));
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!
  댓글 수: 3
Tomas Jurena
Tomas Jurena 2012년 5월 30일
Thank you for your comment. It really seems to be a bug, because running a MEX function causes MATLAB crash as well. I'll contact Tech support.
Tomas Jurena
Tomas Jurena 2012년 5월 30일
OK, no need to do that :) See the answer given by Fred Smith.

댓글을 달려면 로그인하십시오.

채택된 답변

Fred Smith
Fred Smith 2012년 5월 30일
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;
field_width=int32(20);
precision=int32(3);
coder.ceval('printf',s1,s2,field_width,precision,time,s3,int32(iter));
Is your code different?
-Fred
  댓글 수: 2
Tomas Jurena
Tomas Jurena 2012년 5월 30일
Yes, it is just a little bit different to yours, but the difference explains the origin of the problem. Both field_width and precision must be cast to an integer type. Now, it works for me, too. Shame on me, that I forgot about it :) Thank you very much!
Kevin Berndsen
Kevin Berndsen 2015년 3월 18일
Great workaround for the string termination problem. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by