How to extract specific data from a .txt file
3 ビュー (過去 30 日間)
古いコメントを表示
Hi Everyone,
I am trying to extract certain data which is location specific from a .txt file. The text file is not uniformly arranged & contains all kind of characters.
In my case I am looking to extract value of Fx & Fy in row 17 of the FILE.txt file. I have also attached the .txt file.
Looking forward to get it resolved.
Thank you & Reagrds,
0 件のコメント
採用された回答
Voss
2023 年 1 月 31 日
type FILE.TXT % show the contents of FILE.TXT, for reference
data = readlines('FILE.TXT'); % read the file
Here's one way to get the values of FX and FY from line 17:
line_to_get = 17;
result = regexp(data{line_to_get},'(\w+)= ?([\.\dE+-]+)','tokens');
result = vertcat(result{:})
result = str2double(result(ismember(result(:,1),{'FX','FY'}),2))
The same regular expression can be used to get other stuff out of the file too:
line_to_get = 10;
result = regexp(data{line_to_get},'(\w+)= ?([\.\dE+-]+)','tokens');
result = vertcat(result{:})
line_to_get = 11;
result = regexp(data{line_to_get},'(\w+)= ?([\.\dE+-]+)','tokens');
result = vertcat(result{:})
You would use str2double to convert what you need from the second columns of those cell arrays into numbers.
7 件のコメント
Voss
2023 年 2 月 1 日
result = regexp(data{line_to_get},'(\w+)= ?([\.\dE+-]+)','tokens');
Here's a breakdown of that regular expression:
(\w+)= ?([\.\dE+-]+)
% ^^^ match 1 or more alphabetic, numeric, or underscore character(s)
% ^ followed by an equal sign
% ^^ followed by an optional space
% ^^^^^^^^^^ followed by one or more: periods (\.), decimal digits (\d), capital "E"s, plus signs (+), and/or minus signs (-)
% ^ ^ use parentheses to capture and return the parameter name
% ^ ^ use parentheses to capture and return the parameter value
Line 18 looks like this:
+-----------------------------------------------------------------------------+
so it should not match that regular expression. In other words, it's not surprising that regexp returned no matches with that line.
Note that you can use that same regular expression on the contents of the entire file to capture all the parameter names and values, as I showed in the first comment to my answer.
その他の回答 (1 件)
Walter Roberson
2023 年 1 月 31 日
filename = 'FILE.TXT';
S = fileread(filename);
XY = str2double(regexp(S, '(?<=F[XY]= )\S+', 'match'))
3 件のコメント
Walter Roberson
2023 年 1 月 31 日
[XY] matches something that is either X or Y. F[XY]= matches F then either X or Y then = then space. The (?<= ) part means that the FX= or FY= must be present in the input stream and to position immediately after that, but that the text is not to be included in what is returned from the function. It positions the input stream but it does not extract anything from the input stream.
The \S+ matches any number of non-whitespace characters.
So the code looks for FX= or FY= in the input stream, and extracts the next column to be returned from the function. This searching is repeated as long as there are more occurances in the input stream, so both FX and FY would be extracted.
The return from regexp() with 'match' is going to be a cell array of character vectors. Those character vectors are passed to str2double() to be converted to numeric form.
A B C FX= 1325.3 Q= -5 FY= -23.2 P ZFY= 83
The expression as coded does not know to look for whitespace before the variable, so the ZFY= would be matched as being an occurance of FY= . So the regexp() would return {'1325.3', '-23.2', '83'} in this particular case. That could be adjusted if it mattered (but it doesn't matter to you.)
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!