I want t write a number to a file

3 次查看(过去 30 天)
I want to do this:
fOutput = fopen('test.txt','wt');
fprintf(fOutput,'%5.2d',22.56);
fclose(fOutput);
but the txt file is empty after doing this

采纳的回答

Voss
Voss 2022-4-2
It seems to work ok here:
fOutput = fopen('test.txt','wt');
fprintf(fOutput,'%5.2d',22.56);
fclose(fOutput);
ls *.txt
test.txt
type test.txt
2.26e+01
It could be that too many files are open already, e.g., because of code that has previously been run.
You can do fclose all to close all open files and then try to write to test.txt again.
  2 个评论
Jonas Müller
Jonas Müller 2022-4-2
Ok thanks. What means '%5.2d' ? With '%s' it works but i want a float number
Voss
Voss 2022-4-2
%5.2d
% ^ beginning of formatted text signifier
% ^ 5 characters wide (total width)
% ^^ 2 characters after the decimal point (precision)
% ^ decimal number
%5.2f
% ^ floating-point number
fprintf(1,'%5.2d',22.56)
2.26e+01
fprintf(1,'%5.2f',22.56)
22.56

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by