フィルターのクリア

How to use coder.ceval to fprintf a new line ?

3 ビュー (過去 30 日間)
Giulia Coluccini
Giulia Coluccini 2015 年 6 月 26 日
回答済み: Giulia Coluccini 2017 年 8 月 21 日
Matlab R2012b does not support fprint for code generation, so I am using coder.ceval to call the C fprintf function. The problem is on printing a new line, infact the instruction coder.ceval(‘fprintf’, f, ‘%lf\n‘, 10.0); produces as output in the file 10.000000\n €ÿ Moreover the instructions coder.ceval(‘fprintf’, f, ‘%lf\n‘, 10.0); coder.ceval(‘fprintf’, f, ‘%lf\n‘, 20.0); produce as output in the file 10.000000\n&bÿ20.000000\n
How is possible to get the new line?
It can be useful to underline that using printf instead of fprintf the problem does not exist

採用された回答

Giulia Coluccini
Giulia Coluccini 2017 年 8 月 21 日
At the end I solved this problem after several attempts. I discovered that char(10) is the symbol to obtain a new line.
Use these lines:
fname = coder.nullcopy(char(zeros(1,32)));
coder.ceval('sprintf', coder.wref(fname), c_string('FILE_NEW.txt'));
f = coder.opaque('FILE*', 'NULL');
f = coder.ceval('fopen', fname, c_string('a')); coder.ceval('fprintf', f, c_string('%s %s'), c_string('Hello I am Giulia'), c_string(char(10)));
coder.ceval('fclose', f);
where the c_string function is defined as follows:
function y = c_string(s)
y = [s char(0)];
end
It works in Matlab R2012b.

その他の回答 (1 件)

ilker ozturk
ilker ozturk 2017 年 8 月 1 日
Have you solved this problem? I get same result as your problem. If you solved this problem could you write to me? In addition, When I push to start button in matlab function, Matlab has closed. I think, its a bug but I dont know how to pass this problem. (Matlab relase is Matlab 2012a )....
Have a good day..
  1 件のコメント
Giulia Coluccini
Giulia Coluccini 2017 年 8 月 21 日
編集済み: Giulia Coluccini 2017 年 8 月 21 日
Yes, I solved this problem after several attempts. I discovered that char(10) is the symbol to obtain a new line.
Use these lines:
fname = coder.nullcopy(char(zeros(1,32)));
coder.ceval('sprintf', coder.wref(fname), c_string('FILE_NEW.txt'));
f = coder.opaque('FILE*', 'NULL');
f = coder.ceval('fopen', fname, c_string('a')); coder.ceval('fprintf', f, c_string('%s %s'), c_string('Hello I am Giulia'), c_string(char(10)));
coder.ceval('fclose', f);
where the c_string function is defined as follows:
function y = c_string(s)
y = [s char(0)];
end
It works in Matlab R2012b.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by