Format file Data?

4 ビュー (過去 30 日間)
Karan Sandhu
Karan Sandhu 2016 年 4 月 1 日
編集済み: per isakson 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
  1 件のコメント
Karan Sandhu
Karan Sandhu 2016 年 4 月 1 日
Thank you, friend.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by