How to fix the decimal alignment and save the file in ascii regular file?

Hi everyone! I had interpolated the data of 2-D Matrix of 50 columns and 4453 rows. I need to save that data in .ascii file without missing the decimal alignment in a regular order.I attached the output file.
But while writing the output data file the decimal points are getting mis-aligned.
Ex:
9.3 4.8 7.4 5.6
10.4 2.3 11.5 4.9
2.6 4.4 10.4 3.0
Can you please help us with a solution ?

3 Comments

According to my knowledge, you text file is already a ascii file. You have it already....what you expect?
That case you need to show us the code....how you wrote them in a text file.....and what is your expecting order.
"But while writing the output data file the decimal points are getting mis-aligned."
You have a tab-separated file, and the way that most (all?) text editors display tab-separated files is to align the leading edge of each column, not the trailing edge. So this alignment is an artifact of how the file is displayed, not how the file is written.
My answer shows you a workaround that might be suitable for you.

Sign in to comment.

 Accepted Answer

Try this:
M = [9.3,4.8,7.4,5.6;10.4,2.3,11.5,4.9;2.6,4.4,10.4,3];
fmt = repmat('\t%4.1f',1,size(M,2));
fmt = [fmt(3:end),'\n'];
[fid,msg] = fopen('newfile.txt','wt');
assert(fid>=3,msg)
fprintf(fid,fmt,M.')
fclose(fid);
With your example data it gives this:
9.3 4.8 7.4 5.6
10.4 2.3 11.5 4.9
2.6 4.4 10.4 3.0
Note that this adds leading space characters to the values, so the file no longer has a pure tab-separated format. You will also need to pick the fieldwidth 4 to suit the longest printed data value.

More Answers (2)

To align the decimal points, leave the required space:
data = (round(randn(5, 5) * 1000, 1)); % Some test data
data(data == 0) = eps; % To support the logarithm
digit = max(ceil(log10(data)), [], 1);
fmt = sprintf('%%%d.1f ', digit+3)
sprintf([fmt, '\n'], data.')
This find the maximum number of digits required for each column. Then e.g. sprintf('%-7.1f') prints a number with 1 decimal and space for 4 digits, the decimal point, the fractional part and a sign (a total of 7 characters).
Hi
After interpolation, My output file is written in data(original) and modified data(interpolated). I want to save only the modified data in ascii format with 16f8.1 format. The output file of ascii text is of exponential form. I need to have in regular values with 16 entities.
I have attached the script code named cherry.m and the output file is ff_real2.txt.
How can I get that?

6 Comments

"How can I get that?"
Do not use save: just adapt the code that my answer shows you.
Thank you so much Stephen ! Your script helped me a lot !
Thanks once again!
Hi
After editing the script, the first column in the output text file is not aligned perfectly. I attached my output file. Please help me to adjust the first column in decimal alignment!
fmt = repmat('\t%4.1f',1,size(modified_data,2));
fmt = [fmt(3:end),'\n'];
[fid,msg] = fopen('ff_d.txt','wt');
assert(fid>=3,msg)
fprintf(fid,fmt,modified_data.')
fclose(fid);
As I wrote in my comment to your original question, tab-delimited files are displayed differently depending on the text editor/viewer, not because of the how the file itself is formatted. Some text editors (e.g. Notepad++) display tabs with a constant width, while others (e.g. Windows Notepad) horizontally align the trailing edge of the tab characters (so that they appear to be in columns). You do not give any information on what editor/viewer/application you are using to view this file, so it is impossible to say how you should "fix" this. Also note that is you "fix" it for one kind of viewer then it will "break" for the other kind of viewer (or indeed any viewer that does not follow exactly the same tab character handling as the viewer that you are using).
One possible solution would be to remove the tab character entirely and only use fixed-width space characters, i.e. to make each field a fixed width. You can do this easily using the format string, e.g.:
fmt = repmat('%5.1f',1,size(modified_data,2));
fmt = [fmt,'\n'];
Remember that you will need to select the field width to suit the longest field of your data!
Yes ! You are right ! I'm using the Linux(Ubuntu 16.06 LTS ) OS. I'm opening the files in gedit. So please fix this issue.
@Revathi Gandiboyina: use a fixed width file. My last comment shows you how.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!