Format file Data?

11 次查看(过去 30 天)
Karan Sandhu
Karan Sandhu 2016-4-1
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)

采纳的回答

per isakson
per isakson 2016-4-1
编辑:per isakson 2016-4-1
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

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by