Format file Data?

11 vues (au cours des 30 derniers jours)
Karan Sandhu
Karan Sandhu le 1 Avr 2016
Modifié(e) : per isakson le 1 Avr 2016
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)

Réponse acceptée

per isakson
per isakson le 1 Avr 2016
Modifié(e) : per isakson le 1 Avr 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
  1 commentaire
Karan Sandhu
Karan Sandhu le 1 Avr 2016
Thank you, friend.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by