File: ef230-2023-08/www/examples/file_text_table.m Download
%! Example of creating a text file with a table of data
% University of Tennessee : Will Schleter
clear all; clc;
format compact;

a = 5; b = 8.7; c = 3.3; d = [17.2 -5.0 0.0 1.0]; e = [ 8.0; 5.5];
f = [1 pi 2]; g = [8 0 5; 4 8 2]; m = [1.3 3.0 5.2; 2.7 4.5 6.8];  n = [2 4; 9 8];

mean(d)
mean(g)
max(f)

[AA BB] = min(d)

[CC DD] = max(m)

fprintf('Force = %5.1f lbs.\n',a);

fprintf('Force = %5.1f lbs.\n',b);

fprintf('Force = %5.0f lbs.\n',b);

fprintf('Force = %5.0f lbs.\n',e);

fprintf('Force = %5.0f lbs. + %5.0f lbs. + %5.0f lbs.\n',g);

% 2. (80 points) Use Matlab to create the table of data given in problem 2 of HW 4.1. Additional
% requirements / notes.
% a) You need to add a title to the table that includes your name.
% b) The table needs to be printed to both the Command Window and to a text file on the H:\
% drive.
% c) Everything must be in one m.file.
% d) You may edit your solution to problem 2 of HW 4.2, but don’t forget to update the
% documentation.
% Turn in: 1. The worksheet with Problem 1 completed and the above problem statement.
% 2. A print out of your completed and documented Matlab m.file.
% 3. A print out of your two Matlab generated plots
% You DO NOT have to turn in a cover sheet on engineering paper with the problem statement for this
% problem.

% setup data
temp = [ 295 392 463 628 832 949 1285 ];
heat = [1 2 4 8 20 40 80 ];

% init names
fname = 'junk.txt';
myname = 'Joe Blow';

%open file for text output
fp = fopen(fname,'wt');

%ouptput title with name and date
fprintf(fp,'File output by %s\n',myname);
fprintf(fp,'Printed on %s\n\n',datestr(now,0));

%output table headings
fprintf(fp,'Temperature     Heat Generated\n');
fprintf(fp,'(Degrees F)     (x 1000 Btu/ft^3)\n');
fprintf(fp,'-----------     -----------------\n');

%output data
fprintf( fp,'   %4.0f                %2.0f\n', [temp;heat] );

%close file
fclose(fp);

%output to command window
type(fname);

%clean up the silly file
delete(fname);