Anybody w/ new release since R2012b test for resolution of fixed-width field?
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
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 件のコメント
Star Strider
2014 年 6 月 25 日
Not fixed. I get the same result for cc in R2014a.
Star Strider
2014 年 6 月 25 日
My pleasure! The fixed-field format problem has been a problem forever. Even BASIC has it, IIRC.
dpb
2014 年 6 月 25 日
Star Strider
2014 年 6 月 26 日
All I did was to verify your result!
dpb
2014 年 6 月 26 日
回答 (2 件)
dpb
2014 年 6 月 26 日
2 投票
4 件のコメント
Star Strider
2014 年 6 月 26 日
編集済み: Star Strider
2014 年 6 月 26 日
Does it work on your TEST.TXT file? (Haven’t tried it yet myself.)
I noticed is that the data in the other file was ‘sort of’ delimited by all the values being negative. Its success could be limited to such situations.
Star Strider
2014 年 6 月 27 日
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.
dpb
2014 年 6 月 27 日
per isakson
2014 年 6 月 27 日
編集済み: per isakson
2014 年 6 月 29 日
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 件のコメント
dpb
2014 年 6 月 28 日
per isakson
2014 年 6 月 29 日
編集済み: per isakson
2014 年 6 月 29 日
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.
dpb
2014 年 6 月 29 日
この質問は閉じられています。
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!