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)
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
dpb
dpb on 26 Jun 2014
But it answered the question... :)
Never mind that Per a little while later but the kibosh on the whole subject!!! :)

Answers (2)

dpb
dpb on 26 Jun 2014
Well, talk about timing and coincidence!!! Just a few hours ago another query of the nature was posed at
and Per Isakson solved it (or at least provided a workaround) and all other cases like it for all time, apparently!!! The key is one has to give the format specfication as '%w.pf' including the precision as well as the the width as in '%wf' to give the scanner the extra help it needs to not trespass into the initial field of the next value.
Now why this is so remains a mystery, but at least it does get us somewhere on being able to parse a file. As I noted in my comments to Per, I don't know why I never seemed to have thought of doing so explicitly in all these years of complaining (but then again, nobody else pointed it out as a lack, either).
Anyway, again, kudos to Per!!!
  4 Comments
Star Strider
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.
dpb
dpb on 27 Jun 2014
"Great minds" and all that, S-S... :)
I just submitted a comment/request as an addendum to the previous service request that suggested almost identically the same thing as the recommended "fix". I think there should be a more global link somewhere besides just textscan that addresses fixed-width field parsing, but if it were an example therein that would at least be something.
I would still like to learn whether this is consistent with C Standard behavior or if it is inconsistent with C or of it's not specifically addressed by the Standard. So far I've had no success at all in trying to determine that.

per isakson
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
  4 Comments
per isakson
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.
dpb
dpb on 29 Jun 2014
Unfortunately, since it's been 25 yr or more since I first started the campaign with TMW, it's pretty clear they have no intention of doing so now despite that fixed-format files are never going to disappear entirely. I'm now past using Matlab (or any other computing engine, either, for that matter) in anger so it's just no longer worth the effort to keep up the good fight.
And, no, as mentioned somewhere above, I apparently irretrievably lost the mex-file stuff in the move from TN back to the farm as apparently I had only an extant copy on the workstation at the TVA offices where the EPRI I&C Center was housed and I didn't realize it at the time that it wasn't also on the personal machines that I brought. By the time I did miss it, the I&C Center had been closed also and the machines there re-purposed or scrapped so there was no chance to retrieve it, unfortunately.

Products

Community Treasure Hunt

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

Start Hunting!