How do I perform a formatted read on exponential data?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Rik
le 26 Oct 2022
Modifié(e) : Rik
le 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{1}
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
0 commentaires
Mathieu NOE
le 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
0 commentaires
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!