Format file Data?
11 views (last 30 days)
Show older comments
Hi, I need some help in formatting data from an text file from Excel. I've been trying to get the data into spaced out columns, so that they are directly under the fprintf statement that contains headings for the numbers, and it just hasn't been successful. Any help is appreciated. Here is my code:
fprintf('Original Design Ratings\n\n')
fprintf('Design # Cost Durability Function Marketability Total\n')
x=xlsread('Data_11_1');
disp(x)
0 Comments
Accepted Answer
per isakson
on 1 Apr 2016
Edited: per isakson
on 1 Apr 2016
First attempt
num = xlsread( 'Data_11_1' );
fprintf('Original Design Ratings\n\n')
fprintf('%14s%14s%14s%14s%14s%14s\n' ...
, '#Design','Cost','Durability','Function','Marketability','Total')
%
for jj = 1 : size( num, 1 )
fprintf('%14.0f%14.0f%14.0f%14.0f%14.0f%14.0f\n', num(jj,:) )
end
which outputs
#Design Cost Durability Function Marketability Total
101 7 5 3 8 23
102 10 8 9 7 34
103 4 9 5 9 27
104 8 7 10 6 31
105 2 10 7 5 24
Next step is to trim the width of the columns (if needed). Here the widths are controlled by the string "Marketability", which is 13 characters.
Maybe it's easier to trim the columns (not introduce errors) with this code
header_spec = '%14s%14s%14s%14s%14s%14s\n';
data_spec = '%14.0f%14.0f%14.0f%14.0f%14.0f%14.0f\n';
fprintf('Original Design Ratings\n\n')
fprintf( header_spec, '#Design','Cost','Durability','Function','Marketability','Total')
%
for jj = 1 : size( num, 1 )
fprintf( data_spec, num(jj,:) )
end
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!