Issue with dlmwrite tab spacing
5 views (last 30 days)
Show older comments
I am sort of dealing with an old problem which I though I have resolved, but seems not.
What I am trying to do is to write an ascii which would resemble like the following lines if we open that in matlab:
ABC 1.2
This is some text
ID1 ID2.DATA.2015082112 DATE LAT LON ID6.DATA.2015082112 ID7.DATA.2015082112 ID8.DATA.2015082112 ID9.DATA.2015082112 ID10.DATA.2015082112 ID11.DATA.2015082112 ID12.DATA.2015082112
12345 13 201508211200 84.00 252.40 0 0 0.00000 0.00000 0.00000 0.00000 0
12346 13 201508211200 74.70 265.10 0 0 0.00000 0.00000 0.00000 0.00000 0
and here is what I managed to script to get that:
fid=fopen('myfile.obs','w');
fprintf(fid,'ABC 1.2\n');
fprintf(fid,'Observations de cumuls de precipitation pour CaPA\n');
fprintf(fid,['ID1 ID2.DATA.2015082112...
' DATE LAT LON ID6.DATA.2015082112...
' ID7.DATA.2015082112...
' ID8.DATA.2015082112...
' ID9.DATA.2015082112...
' ID10.DATA.2015082112...
' ID11.DATA.2015082112...
' ID12.DATA.2015082112 '\n']);
fclose('all');
dlmwrite('myfile.obs', Mat,'-append', 'delimiter', '\t', 'precision', 12);
where mData in my matlab data. I have used ('precision', 12) cause in Mat (my data table), column 3 which contains the dates, are in scientific notation and that is the only way I can force the program to write it in the way I want. The above code results in:
ABC 1.2
This is some text
ID1 ID2.DATA.2015082112 DATE LAT LON ID6.DATA.2015082112 ID7.DATA.2015082112 ID8.DATA.2015082112 ID9.DATA.2015082112 ID10.DATA.2015082112 ID11.DATA.2015082112 ID12.DATA.2015082112
12345 13 201508211200 84.00 252.40 0 0 0 0 0 0 0
12346 13 201508211200 74.70 265.10 0 0 0 0 0 0 0
You see the differences? Tab spacings are not equal in what I have got to what I am trying to get. There are some issues with decimal places in some columns which are of less concern to me, but any thoughts on either of these issues is highly appreciated, more specifically the spacing issue!
0 Comments
Answers (1)
dpb
on 22 Aug 2015
Edited: dpb
on 22 Aug 2015
If you want a specific format including spacing, then use a specific format string with fprintf to write the file. "Higher-level" routines offer some convenience but at a price of not providing precise control over all aspects of the formatting.
Define what you want for each numeric field and we can write a suitable format string (or you can, too, for that matter, simply read the doc for fprintf and see the examples)
ADDENDUM
To demonstrate the process in general...here's the desired output line format after pasting in a code editor and inserting for convenience column numbers...
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111
000000000111111111122222222223333333333444444444455555555566666666666777777777788888888889999999999000000000111111111122
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
12345 13 201508211200 84.00 252.40 0 0 0.00000 0.00000 0.00000 0.00000 0
From this we can ascertain type and format for the various fields. First, the columns for which fields end are seen to be
5,17,31,39,48,54,56,72,86,100,114,118
and the format for each corresponding field type and precision (ignoring width is
%d,%d,%d,%.2f,%.2f,%d,%d,%.5f,%.5f,%.5f,%.5f,%d
Now looking at the width of each field, which is the diff() of the above column endings, we find
>> c=[0,5,17,31,39,48,54,58,72,86,100,114,118];
>> diff(c)
ans =
5 12 14 8 9 6 4 16 14 14 14 4
>>
Unfortunately, only the last few %f fields are consistent so we can't make much use of anything but the actual widths. So the format string can be written as
fmt=['%5d%12d%14d%8.2f%9.2f%6d%4d ' repmat('%14.5f',1,4) '%4d\n'];
The above will preserve precisely the width and precision of the sample text line. If it were me unless this is set by some other application, I'd clean up the above mismatches of approximately-equal widths so could simplify to fewer individual fields and make more use of the repmat construct, but that's just a nicety. You should get the idea from the example.
0 Comments
See Also
Categories
Find more on Transfer Function Models 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!