How to fix the decimal alignment and save the file in ascii regular file?
Show older comments
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
KSSV
on 21 Mar 2018
According to my knowledge, you text file is already a ascii file. You have it already....what you expect?
KSSV
on 21 Mar 2018
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.
Accepted Answer
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).
Revathi Gandiboyina
on 22 Mar 2018
Edited: Revathi Gandiboyina
on 22 Mar 2018
0 votes
6 Comments
Stephen23
on 22 Mar 2018
"How can I get that?"
Do not use save: just adapt the code that my answer shows you.
Revathi Gandiboyina
on 22 Mar 2018
Revathi Gandiboyina
on 23 Mar 2018
Edited: Stephen23
on 23 Mar 2018
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!
Revathi Gandiboyina
on 24 Mar 2018
Stephen23
on 24 Mar 2018
@Revathi Gandiboyina: use a fixed width file. My last comment shows you how.
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!