How do I perform a formatted read on exponential data?

5 views (last 30 days)
I want to read a few hundred thousand lines of data. Here are the first few lines:
TABLED1 1
+ 0.000+0-0.000+0 1.000-2-3.297+1 2.000-2-4.924+1 3.000-2-5.692+1+
+ 4.000-2-1.301+2 5.000-2-1.128+2 6.000-2-1.031+2 7.000-2-9.388+1+
+ 8.000-2-8.941+1 9.000-2-9.161+1 1.000-1-9.107+1 1.100-1-9.013+1+
I've searched, read answers and tried textscan() with different options as well as some even less successful methods of reading data - all to no avail. textscan() doesn't recognize '%e' and '%f' doesn't return the desired result. I've even tried reading one line at a time and writing my own parsing algorithm. What I wouldn't give for a good Fortran read() statement! lol
Thank you in advance for your time and consideration.
  5 Comments
Matthew Koebbe
Matthew Koebbe on 26 Oct 2022
Rik, As the title indicates, formatted data, namely 10 columns of 8 characters. See FORTRAN, NASTRAN, etc. You know, old folks' stuff. :) If I add spaces to make the data more readable, the first line becomes
0.000+0 -0.000+0 1.000-2 -3.297+1 2.000-2 . . .
You get the idea. This particular data format doesn't require the 'E' or 'e'.
VBBV, Thanks!
Matthew Koebbe
Matthew Koebbe on 26 Oct 2022
Moved: Rik on 27 Oct 2022
I want to thank everyone who has answered. I definitely have learned new things about MATLAB, and I look forward to working through the solutions I haven't gotten to yet. I have other work that demands my attention through COB tomorrow, but I'll give my final solution choice on Friday. Thanks again!

Sign in to comment.

Answers (2)

Rik
Rik on 26 Oct 2022
Edited: Rik on 26 Oct 2022
Edit at the top: You can get the data in this format with my readfile function, or with data=cellstr(readlines(filename));.
There are perhaps more direct ways, but this is how you can do it with a regular expression.
data={'+ 0.000+0-0.000+0 1.000-2-3.297+1 2.000-2-4.924+1 3.000-2-5.692+1+',
'+ 4.000-2-1.301+2 5.000-2-1.128+2 6.000-2-1.031+2 7.000-2-9.388+1+',
'+ 8.000-2-8.941+1 9.000-2-9.161+1 1.000-1-9.107+1 1.100-1-9.013+1+'};
tokens=regexp(data,'-?([0-9\.]+)([\+\-]\d+)','tokens')
tokens = 3×1 cell array
{1×8 cell} {1×8 cell} {1×8 cell}
tokens{1}
ans = 1×8 cell array
{1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell}
output = NaN(numel(tokens),numel(tokens{1}));
for n=1:numel(tokens)
for m=1:numel(tokens{n})
tmp = str2double(tokens{n}{m});
output(n,m) = tmp(1) * 10^tmp(2);
end
end
output
output = 3×8
0 0 0.0100 32.9700 0.0200 49.2400 0.0300 56.9200 0.0400 130.1000 0.0500 112.8000 0.0600 103.1000 0.0700 93.8800 0.0800 89.4100 0.0900 91.6100 0.1000 91.0700 0.1100 90.1300

Mathieu NOE
Mathieu NOE on 26 Oct 2022
Coming a bit too late in the show...
filename = 'tabled1.txt'
header_lines = 1;
data = get_my_data(filename,header_lines)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function data = get_my_data(filename,header_lines)
lines = readlines(filename);
data = []; % concat
for ci = 1+header_lines:numel(lines)
line = char(lines(ci));
if numel(line)>1
line = line(end-64:end-1) % keep only 8 numbers coded on 8 characters
nums_one_line = [];
for cc = 1:8
ind1 = 1+(cc-1)*8;
ind2 = ind1+7;
numb = line(ind1:ind2); % example : '-8.941+1'
% + or - for exponant is in position 7
mantissa = str2double(numb(1:6));
expo = str2double(numb(7:8));
num = mantissa*10^(expo);
nums_one_line = [nums_one_line num]; % concat
end
data = [data; nums_one_line]; % concat
end
end
end

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!