Trying to get text file to align properly for data table

11 views (last 30 days)
uData is a 30x5 matrix with numbers. name_Database is a 30x1 cell array with strings of Names (e.g. Fake Subject 1, Fake Subject 2, Bob). What would fix the code so that the data aligns? (please see image for the misalignment).
ID = dbedit.uData(:,1);
scE = dbedit.uData(:,2);
ccE = dbedit.uData(:,3);
scC = dbedit.uData(:,4);
ccC = dbedit.uData(:,5);
names = dbedit.name_Database;
% Text file to output data into is called uDatabase.txt file.
output_file = 'uDatabase.txt';
% Open file for writing
fid = fopen(output_file, 'w+');
% Header
fprintf(fid, '%6s %12s %18s %24s %30s %36s\n', 'Name', 'ID', 'scE',...
'ccE', 'scC', 'ccC');
% Write the data.
for ii=1:numel(names)
fprintf(fid, '%6s %12.0f %18.0f %24.0f %30.0f %36.0f\n',names{ii},...
ID(ii),scE(ii),ccE(ii),scC(ii),...
ccC(ii));
end
% Close the .txt file.
fclose(fid);
Text file:
Name ID scE ccE scC ccC
Fake Subject 1 1 3 4 5 2
Fake Subject 2 2 4 3 2 5
Fake Subject 3 3 1 6 6 1
Fake Subject 4 4 2 5 3 4
Fake Subject 5 5 3 4 4 3
Fake Subject 6 6 6 1 1 6
Fake Subject 7 7 7 0 1 6
Fake Subject 8 8 4 3 3 4
Fake Subject 9 9 2 5 5 2
Fake Subject 10 10 5 2 4 3
Fake Subject 11 11 6 1 3 4
Fake Subject 12 12 5 2 2 5
Fake Subject 13 13 1 6 3 4
Fake Subject 14 14 3 4 4 3
Fake Subject 16 15 2 5 3 4
Fake Subject 17 16 1 6 1 6
Fake Subject 18 17 4 3 3 4
Fake Subject 19 18 2 5 5 2
Fake Subject 20 19 6 1 5 2
Fake Subject 21 20 3 4 1 6
Fake Subject 22 21 3 4 3 4
Fake Subject 23 22 5 2 1 6
Fake Subject 24 23 3 4 1 6
Fake Subject 25 24 7 0 3 3
Fake Subject 26 25 3 4 3 4
Fake Subject 27 26 0 0 0 0
Fake Subject 15 27 4 3 3 3
Bob 28 6 1 2 5
ludba 29 4 3 4 3
Test Subject 2 30 0 0 0 0

Accepted Answer

Star Strider
Star Strider on 19 Dec 2014
One simple fix may be just to expand the field that writes the names to 15 or more characters:
fprintf(fid, '%16s %12.0f %18.0f %24.0f %30.0f %36.0f\n',names{ii},...
ID(ii),scE(ii),ccE(ii),scC(ii),...
ccC(ii));
changing the ‘%6s’ to ‘%16s’, for example.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!