Reading numeric values from complex text files
Afficher commentaires plus anciens
I know this question has been asked too many times before, but I'm attempting to read numeric values from a text file that has the following format:
2.00000000000000e+001 (-2.58645139980833e+001dB,-3.39749468897168e+001°)
(This format comes from LTspiceIV AC analysis, edit: see attached .txt for example file). I have tried using different functions to read this (dlmread, fscanf, textscan) but with no success. The method I tried was to use multiple delimiters e.g.
{'\t','dB','°','(',')',','}
textscan(filename,'%f %f %f','HeaderLines',1,'Delimiter',{'\t','\n','dB','°','(',')',','});
and also grouped e.g.
textscan(filename,'%f %f %f','HeaderLines',1,'Delimiter',{'\t(','dB,','°)'});
and also with a format specifier including the whole line make-up in fscanf e.g.
'%f\t(%fdB,%f°)'
I also need to skip 1 header row. Where am I going wrong?
1 commentaire
Can you please upload the complete file. Without this it is difficult to know how the lines are arranged, how the values repeat, the format of the lines, and other information that we need to know how the file should be parsed.
Your title describes them, as being "complex text files", but we do no have any information on their format.
You can upload a file by clicking on the paperclip button and then both the Choose file and Attach file buttons.
Réponse acceptée
Plus de réponses (1)
Thorsten
le 7 Oct 2015
You can process individual lines using
s = fgets(fid);
data(i,:) = sscanf(s, '%f (%fdB, %f)');
2 commentaires
Ben Holmes
le 7 Oct 2015
The problem is due to the degree sign in the file. Without it, you can use
data = textscan(fopen('code.m'), '%f\t(%fdB,%f)', 'Headerlines', 1)
Catégories
En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!