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

In summary, the conversation is discussing how to fix a code that is causing misalignment in a 30x5 matrix with numbers and a 30x1 cell array with strings. The solution involves using %d conversion specifiers instead of %f and understanding how the number in a conversion specifier affects the width of the field.
  • #1
muaaman
5
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
  • #2
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
 

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

Why is my text not aligning properly in my .txt file when using Matlab?

The most likely reason for this issue is that the text in your .txt file is not formatted correctly. Make sure that you are using the correct delimiters (such as tabs or commas) between columns, and that the columns are all the same width. Additionally, check that the font and font size of the text in your .txt file matches the settings in your Matlab code.

How can I align text in a table format in a .txt file using Matlab?

To align text in a table format in a .txt file using Matlab, you can use the "fprintf" function. This function allows you to specify the column width and alignment for each element of the table. You can also use the "sprintf" function to format the text before writing it to the .txt file.

What is the best way to troubleshoot alignment issues in a .txt file created with Matlab?

The best way to troubleshoot alignment issues in a .txt file created with Matlab is to first check the formatting of the text, as mentioned in the first question. If the formatting is correct and the issue persists, try adjusting the column widths and alignment settings in your code. You can also try using different delimiters or changing the font and font size of the text in your .txt file.

How can I save a table with aligned text in a .txt file using Matlab?

To save a table with aligned text in a .txt file using Matlab, you can use the "dlmwrite" function. This function allows you to specify the delimiter, precision, and alignment for each element of the table. Alternatively, you can use the "fprintf" or "sprintf" functions as mentioned in the second question.

Can I align text in a .txt file using Matlab without affecting the original data?

Yes, you can align text in a .txt file using Matlab without affecting the original data. This can be achieved by using the "sprintf" function to format the text and then writing it to a new .txt file. This way, the original data in the .txt file will remain unchanged, and you will have a new file with the aligned text.

Similar threads

Back
Top