coder.ceva​l('printf'​,....) does not print a message instantaneously

2 次查看(过去 30 天)
Hi! I need to print a message every few iterations during a simulation to control current state of the simulation. Since the m-file is designated for C-code generation using codegen, I use coder.ceval('printf',...) method. When I verify the compiled C-code using .mex function, none of the messages is printed during the simulation. Instead, all messages are printed at once after the simulation finishes. How can I force codegen to print a message instanteneously? Does anything similar to drawnow exist for coder.ceval('printf',..)? Anyone's help is appreciated!

采纳的回答

Friedrich
Friedrich 2012-8-17
编辑:Friedrich 2012-8-17
Hi,
as long you stay with mex you can do a
mexEvalString("drawnow");
So a
coder.ceval('mexEvalString', '"drawnow"')
This will give the MATLAB Thread enough time to flush the buffer and to display the data.
In the case you want to generate code not for mex only you can use the coder.target to differentiate between those cases during the code generation process:
if strcmp('mex',coder.target)
coder.ceval('mexEvalString', '"drawnow"')
end
  1 个评论
Tomas Jurena
Tomas Jurena 2012-8-20
Thank you for your answer, but unfortunately this did not help. Here is my code:
matlab_run = isempty(coder.target);
mex_run = strcmp('mex',coder.target);
. . . . .
. . . . .
. . . . .
if matlab_run
fprintf('some message');
else
coder.ceval('printf',C_formatted_message);
if mex_run
coder.ceval('mexEvalString','"drawnow"');
end
end
It still does not print messages during the simulation. Is anything wrong in the code? Note that when the generated C-code is executed from a main function, messages are printed to the console instantaneously.

请先登录,再进行评论。

更多回答(1 个)

Tomas Jurena
Tomas Jurena 2012-8-20
编辑:Tomas Jurena 2012-8-20
OK, I've finally figured it out with Friedrich's help. The argument of coder.ceval must be a C-formatted string, i.e.
C_command = ['drawnow' char(0)];
coder.ceval('printf',C_formatted_message);
coder.ceval('mexEvalString',C_command);
  1 个评论
Juan Rojas
Juan Rojas 2013-2-20
how do you create the C_formatted_message?
In my case I use printf with a file stream as: coder.ceval('printf',fid,s11,s12,int32(i));
Where fid was declared as: fid = coder.opaque('FILE *','NULL');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Generating Code 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by