フィルターのクリア

Reading numeric values from complex text files

2 ビュー (過去 30 日間)
Ben Holmes
Ben Holmes 2015 年 10 月 7 日
コメント済み: Ben Holmes 2015 年 10 月 8 日
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 件のコメント
Stephen23
Stephen23 2015 年 10 月 7 日
編集済み: Stephen23 2015 年 10 月 7 日
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.

サインインしてコメントする。

採用された回答

Jeremy Hughes
Jeremy Hughes 2015 年 10 月 8 日
編集済み: Jeremy Hughes 2015 年 10 月 8 日
Hi Ben,
I found this worked. (of course I used a string and not a file id, but it works the same)
textscan(fid,'%f(%fdB%f°)','Delimiter',{'\t',','},'HeaderLines',1)
Good Luck,
Jeremy
  1 件のコメント
Ben Holmes
Ben Holmes 2015 年 10 月 8 日
Thanks Jeremy, this works great. I think the issue which I was struggling with most was understanding how to format the format specifier which I haven't managed to find a comprehensive explanation for. To my best guess, delimiters should separate the values, but you can also include other repeated characters in the specifier which you want to ignore?

サインインしてコメントする。

その他の回答 (1 件)

Thorsten
Thorsten 2015 年 10 月 7 日
You can process individual lines using
s = fgets(fid);
data(i,:) = sscanf(s, '%f (%fdB, %f)');
  2 件のコメント
Ben Holmes
Ben Holmes 2015 年 10 月 7 日
Thanks! This does work but I was hoping for something more elegant so that I didn't have to iterate. My current code now involves a while loop iterating until the end of the file which ends up with a matrix with constantly changing size:
fileID = fopen(filename,'r');
% Discard header line;
s = fgets(fileID);
n=1; % Setup index
s = fgets(fileID); % Get first line
% While not at the end of the file
while(s ~= -1)
% Extract numbers
data(:,n) = sscanf(s,'%f\t(%fdB,%f°)');
n=n+1; % Increment index
s = fgets(fileID); % get next line
end
It will do the trick but is not good practice...
Thorsten
Thorsten 2015 年 10 月 7 日
編集済み: Thorsten 2015 年 10 月 7 日
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)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by