MATLAB Answers

Exporting the output into a .txt file

2 views (last 30 days)
Berkay SAYGILI
Berkay SAYGILI on 12 Apr 2021
Answered: Walter Roberson on 12 Apr 2021
Hello;
I wrote few lines of code including multiple disp functions to obtain a table in the command window as an output. I want to export the entire output(table in the command window) to a .txt file. How can i achieve this? My code and output are below;
Tmin = -40; %F
Tmax = 40;
Vmax = 60;
Vmin = 10; %mi/h
A = linspace(Tmin,Tmax,9);
disp(' Temperature ')
disp(A)
disp(' Speed ')
V = transpose(10:10:60);
for speed = Vmin: 10: Vmax
for Temp = Tmin:10: Tmax
Twc = int16(35.74 + .6215 * Temp...
-35.75*(speed^0.16) + 0.4275*Temp*(speed^0.16));
fprintf(' %d\t ', Twc)
end
fprintf(' \n ')
end
OUTPUT:
Temperature
-40 -30 -20 -10 0 10 20 30 40
Speed
-66 -53 -41 -28 -16 -4 9 21 34
-74 -61 -48 -35 -22 -9 4 17 30
-80 -67 -53 -39 -26 -12 1 15 28
-84 -71 -57 -43 -29 -15 -1 13 27
-88 -74 -60 -45 -31 -17 -3 12 26
-91 -76 -62 -48 -33 -19 -4 10 25

Answers (1)

Walter Roberson
Walter Roberson on 12 Apr 2021
  • You could evalc() the code, getting back a character vector, that you then wrote to a file.
  • You could fopen() a file, and fprintf() to the file instead of to the display
  • You could use diary()
  • You could potentially use publish(), depending what you were going to do with the file
  • instead of using fprintf() like that, you could use dlmwrite() several times, using append write mode. dlmwrite() is not the best at handling text though
  • you could create cells and use writecell()

Community Treasure Hunt

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

Start Hunting!

Translated by