Info
This question is closed. Reopen it to edit or answer.
Anybody w/ new release since R2012b test for resolution of fixed-width field?
1 view (last 30 days)
Show older comments
I created a Service Request from the subject thread--I just noticed it was closed with a status of "Resolved" but AFAIK there has been no change actually implemented to fix the issue and it appears based on some recent threads here there are some areas that have been broken that did formerly work...
So, can somebody w/ a recent version try any of the last examples there and see if this has actually been fixed or if TMW just closed it because tired of hearing about it?
Sample file attached, code to test would be
cc=cell2mat(textscan(fid,['%5f' repmat('%6f',1,3)],'delimiter',''))
cc =
13.1000 54.6000 99.0000 0.3300
13.1500 54.6010 0.0020 0.3400
13.2000 54.6000 0.0030 0
13.2500 54.6010 28.0000 -40.1600
The correct array is
13.1000 54.6000 99.0000 0.33
13.1500 54.6000 100.0000 200.34
13.2000 54.6000 0.0000 300.00
13.2500 54.6000 128.0000 -40.16
6 Comments
Answers (2)
dpb
on 26 Jun 2014
4 Comments
Star Strider
on 27 Jun 2014
This should be part of the Examples section of the textscan documentation, specifically denoting it as a way to read FORTRAN-type fixed-field files.
per isakson
on 27 Jun 2014
Edited: per isakson
on 29 Jun 2014
Another test without the precision specifier (induced by a comment by dpb in the other post) (R2013a)
N = 5;
format_spec = '%5f%5f%6f%6f';
fid = fopen( 'poi_fixed_format.txt', 'r' );
cac = textscan( fid, format_spec, 'CollectOutput',true );
fclose( fid );
celldisp( cac )
[a,b,c,d]=textread('poi_fixed_format.txt',format_spec, 'delimiter','');
TR = [a,b,c,d]
fid = fopen( 'poi_fixed_format.txt', 'r' );
M = fscanf( fid, format_spec );
fclose( fid );
FS = transpose( reshape( M, [4,4] ) )
outputs
cac{1} =
13.1000 54.6000 99.0000 3.4500
13.1500 54.6000 111.0000 123.4500
13.2000 54.6000 11.0010 23.4500
13.2500 54.6000 1.0012 3.4500
TR =
13.1000 54.6000 99.0000 3.4500
13.1500 54.6000 111.0000 123.4500
13.2000 54.6000 11.0010 23.4500
13.2500 54.6000 1.0012 3.4500
FS =
13.1000 54.6000 99.0000 3.4500
13.1500 54.6000 111.0000 123.4500
13.2000 54.6000 11.0010 23.4500
13.2500 54.6000 1.0012 3.4500
where poi_fixed_format.txt contains
13.10 54.60 99.00 3.45
13.15 54.60111.00123.45
13.20 54.60 11.00123.45
13.25 54.60 1.00123.45
with "1" in position one; no leading spaces
.
Tentative conclusion:
To me it looks as if
- leading spaces of "fields" are skipped; not counted to make up the width
- [+- ] and new-line are interpreted as start of new field; overruns width
This is consistent with both
- scanf Width Specification on C at MSDN Library and
- 4.9.6.2 The fscanf function of http://flash-gordon.me.uk/ansi.c.txt.
4 Comments
per isakson
on 29 Jun 2014
Edited: per isakson
on 29 Jun 2014
Now I read the CSSM-thread, fscanf / sscanf / textscan with fixed format and realize this is an old topic.
My conclusions
- TMW is reluctant to point out deficiencies of Matlab, which explains why reading Fortran-fixed-format text files is not discussed in the documentation.
- TMW will not add a new function to support "Fortran-like FORMAT syntax" since fixed-format files are becoming more rare over time. Why now when TMW didn't do it long ago?
- Reading Fortran-fixed-format text files causes a small(?) number of users trouble and wasted time.
- There is no function to read Fortran-fixed-format files in the File Exchange.
dpb, did you find your "set of utilities that used Fortran mex-files"? They would be make a good FEX-contribution.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!