Filter löschen
Filter löschen

How do I use fprintf to print to a text file?

3 Ansichten (letzte 30 Tage)
TheSaint
TheSaint am 3 Feb. 2024
Beantwortet: Star Strider am 3 Feb. 2024
fprintf(' Force(N) \t Spring Constant (N/m) \t Potential Energy (J) \n')
fprintf(' %1.0f \t\t %2.0f \t\t\t\t\t %0.2f\n', [F; SC; E])
I have a code that looks like this, and it prints to a chart that looks like this:
Force Spring Constant Potential Energy
25 200 1.40
35 450 1.10
20 100 1.44
15 300 0.80
How do I print this chart to a brand new file?

Antworten (2)

Walter Roberson
Walter Roberson am 3 Feb. 2024
filename = "AppropriateName.txt";
[fid, msg] = fopen(filename, "wt");
if fid < 0
error('failed to open file "%s" because "%s"', filename, msg);
end
fprintf(fid, ' Force(N) \t Spring Constant (N/m) \t Potential Energy (J) \n');
fprintf(fid, ' %1.0f \t\t %2.0f \t\t\t\t\t %0.2f\n', [F; SC; E]);
fclose(fid);

Star Strider
Star Strider am 3 Feb. 2024
Use fopen to create a file identification number, change thefprintf statements accordingly, then use fclose to close it.
fid = fopen('YourFileNazme.txt','wt');
fprintf(fid, ' Force(N) \t Spring Constant (N/m) \t Potential Energy (J) \n')
fprintf(fid, ' %1.0f \t\t %2.0f \t\t\t\t\t %0.2f\n', [F; SC; E])
fclose(fid);
It would be easier to create this as a table and then use writetable.
.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by