Who could get all the data in the attached file by the matlab?

2 ビュー (過去 30 日間)
huazai2020
huazai2020 2020 年 7 月 4 日
コメント済み: Walter Roberson 2020 年 7 月 8 日
Who could get all the data in the attached file by the matlab? I do not need the text words, but only need the data in two column?Who can help me?
  7 件のコメント
huazai2020
huazai2020 2020 年 7 月 5 日
It is not Walter's problem, but my data "tem-001.txt", could you import it into one xlsx.file? If yes, could you send me the code, thank you.
Walter Roberson
Walter Roberson 2020 年 7 月 5 日
Take my code and use writematrix or xlswrite to write the data variable to an xlsx file.

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

採用された回答

Stephen23
Stephen23 2020 年 7 月 6 日
>> rgx = '([-+]?\d+\.?\d*([eE][-+]?\d+)?)';
>> str = fileread('tem-001.txt');
>> tkn = regexp(str,[rgx,'\s+',rgx],'tokens');
>> mat = str2double(vertcat(tkn{:}))
mat =
0.025788 0.0045471
0.016141 0.0093192
0.0088788 0.010581
0.0040584 0.01115
-1.2949e-12 0.0112
-0.0040227 0.011052
-0.0090357 0.010768
-0.015782 0.009112
-0.025416 0.0044815

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 7 月 4 日
filename = 'tem-001.txt';
S = fileread(filename);
SS = strjoin( regexp(S, '^\s*-?\d.*$', 'match', 'lineanchors', 'dotexceptnewline'), '\n');
data = cell2mat(textscan(SS, '%f%f'));
  7 件のコメント
huazai2020
huazai2020 2020 年 7 月 8 日
Could you tell me the meaning of each row? What is the difference between below?
>> rgx = '([-+]?\d+\.?\d*([eE][-+]?\d+)?)';
>> str = fileread('tem-001.txt');
>> tkn = regexp(str,[rgx,'\s+',rgx],'tokens');
>> mat = str2double(vertcat(tkn{:}))
Walter Roberson
Walter Roberson 2020 年 7 月 8 日
That is Stephen's code.
rgx is assigned a pattern that will match floating point numbers with optional decimal place and optional exponent. However the pattern misses the possibility of a decimal number with no leading digits before the period.
The assignment to str reads the content of the file all at once and puts it into the variable.
The next line creates a pattern of one number representation followed by whitespace followed by another number representation. Then it searches the content of the file returning the characters that match the patterns.
The matched parts are put together into columns and passed to str2double to convert to numeric.
The workings of patterns for regular expressions is too large a topic for now. There is a file exchange contribution that helps explore regular expressions. The finer points of regular expressions can be pretty tricky.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by