problem reading with format
1 view (last 30 days)
Show older comments
Hi,
I have a text file starting as
1 19 2 3 1 5 4 1 5 6 1 7216.9023.30 2.7023.10 2.8017.40 1.1017.40 0.14 0.3484781 0.2780707 0.2950000 34.6 4.5 33.0 28.3 26.6 23.4 22.3 22.2 23.1 21.0 17.1 15.0 12.5 8.7 5.4 0.0 2.6 0.0
1 23 1 3 1 1 2 1 2 6 5 6720.9027.20 2.2027.20 2.3022.90 1.6022.70 0.15 0.5713450 0.4988143 0.5600000 41.8 3.2 38.2 31.0 27.9 27.2 25.7 24.6 24.8 23.1 21.8 17.9 15.6 11.8 6.9 0.0 2.8 0.0
When I try to read it with format
form=[ '%7.0f %3.0f %3.0f %2.0f %2.0f %2.0f %2.0f %2.0f %2.0f %2.0f' ...
'%2.0f %3.0f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f' ...
'%5.2f %10.7f %10.7f %10.7f %5.1f %5.1f %5.1f %5.1f %5.1f %5.1f' ...
'%5.1f %5.1f %5.1f %5.1f %5.1f %5.1f %5.1f %5.1f %5.1f %5.1f ' ...
'%5.1f %5.1f']
c=textscan(fid,form,863);
Matlab does not read correctly. The field 12 is really 3 characters but Matlab reads in that place 4 characters, so that the 13. number is 6.9 but it should be 16.9. When I put a wrong format for field 12
form=[ '%7.0f %3.0f %3.0f %2.0f %2.0f %2.0f %2.0f %2.0f %2.0f %2.0f' ...
'%2.0f %2.0f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f' .
then Matlab read the file correctly.
What might be the problem?
Answers (1)
Jeremy Hughes
on 3 Jun 2019
Edited: Jeremy Hughes
on 3 Jun 2019
The leading spaces are not being counted toward the width of the fields as you're expecting.
%n.pf - will read at most n characters, starting at the beginning of a field, which does not include the spaces, it will include only the first p characters after the decimal.
You might have better luck with fixedWidthImportOptions. I didn't try this since I don't have your exact file.
>> w = [7 3 3 2 2 2 2 2 2 2 2 3 5 5 5 5 5 5 5 5 5 10 10 10 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5];
>> opts = fixedWidthImportOptions('NumVariables',numel(w),'VariableWidths',w);
>> opts = setvartype(opts,'double');
% In R2019a
>> A = readmatrix(file,opts);
% If using an earlier release,
>> T = readtable(file,opts);
>> A = T.Variables;
Edit: Oops, forgot NumVariables.
2 Comments
per isakson
on 3 Jun 2019
Edited: per isakson
on 3 Jun 2019
I have no luck with T = readtable(file,opts); on R2018a neither with leading spaces in the text file nor without leading spaces
K>> getReport(ME) % at parseInputs (line 28+2)
ans =
'Error using matlab.io.text.FixedWidthImportOptions/set.VariableWidths (line 92)
Expected a vector of positive integers of the same length as VariableNames.
Error in matlab.io.internal.mixin.HasPropertiesAsNVPairs/parseInputs (line 28)
obj.(param{:}) = results.(param{:});
Error in matlab.io.text.FixedWidthImportOptions (line 74)
[opts,otherArgs] = opts.parseInputs(varargin,{'NumVariables','VariableOptions','VariableNames'});
Error in fixedWidthImportOptions (line 37)
opts = matlab.io.text.FixedWidthImportOptions(varargin{:});
Error in LiveEditorEvaluationHelperESectionEval (line 20)
opts = fixedWidthImportOptions('VariableWidths',[1 3 3 2 2 2 2 2 2 2 2 3 5 5 5 5 5 5 5 5 5 10 10 10 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5])
Error in matlab.internal.editor.evaluateCode'
More luck with the interactive Import Data
Isn't there a m-code generation option?
Jeremy Hughes
on 3 Jun 2019
Wrote code without running it /shame.
I forgot to add NumVariables. Edited.
Import Tool generates MATLAB code for fixed width files, but I think there are limits to what it can do.
See Also
Categories
Find more on Data Import and Export 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!