Matlab Issue with aligning text in table format in .txt file

Click For Summary
SUMMARY

The forum discussion addresses a MATLAB issue related to aligning text in a table format when exporting data to a .txt file. The user is working with a 30x5 matrix (uData) and a 30x1 cell array (name_Database) and encounters misalignment due to incorrect usage of conversion specifiers in the fprintf function. The suggested solution involves replacing the %f specifiers with %d for integer data and clarifying the purpose of the width in conversion specifiers, which controls the field width rather than the starting column. A reference link to MATLAB's documentation on formatting strings is provided for further guidance.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with fprintf function for file output
  • Knowledge of data types in MATLAB (e.g., cell arrays, matrices)
  • Basic understanding of formatting strings in programming
NEXT STEPS
  • Review MATLAB's fprintf documentation for formatting strings
  • Learn about MATLAB cell arrays and their manipulation
  • Explore data type conversion in MATLAB, focusing on integer and floating-point types
  • Investigate best practices for exporting data to text files in MATLAB
USEFUL FOR

MATLAB users, data analysts, and students working on data export tasks who need to ensure proper alignment and formatting in text files.

muaaman
Messages
4
Reaction score
0

Homework Statement


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).

unaligned-table_txtfile.png

Homework Equations

The Attempt at a Solution


[/B]

Code:
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);
 
Physics news on Phys.org
It looks to me like most of the data you are printing to the file is integer data, so I would be using %d conversion specifiers in place of the %f conversion specifiers you are using.

Also, I think you have a misconception about what the number in a conversion specifier such as %18.0f does. This does NOT start printing in column 18. What it does is print the floating point number in a field of width 18, with no digits to the right of the decimal point. Because your conversion specifiers are %6s %12s %18s and so on, you get ever increasing spacing between subsequent numbers. The link below discusses how to use conversion specifiers to format strings or number sent to the screen or to a file.

http://www.mathworks.com/help/matlab/matlab_prog/formatting-strings.html
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K