How to read comma separated .txt file in matlab R2018a?

18 ビュー (過去 30 日間)
M
M 2023 年 2 月 26 日
コメント済み: Walter Roberson 2023 年 2 月 26 日
How to read comma separated .txt file in matlab R2018a?
The rows is like this :
And I want only the data which begins in "Sensor1:.#A-R="
Sensor1:.#A-R=-1.00,-20.00,226.00
Sensor2:.#A-R=-13.00,-43.00,297.00
Sensor3:.#A-R=-10.00,-11.00,216.00
Sensor1:.#A-R=-2.00,-18.00,224.00
Sensor2:.#A-R=-15.00,-41.00,298.00
Sensor3:.#A-R=-11.00,-11.00,220.00
Sensor1:.#A-R=-2.00,-17.00,227.00
Sensor2:.#A-R=-15.00,-42.00,298.00
Sensor3:.#A-R=-9.00,-12.00,225.00
Sensor1:.#A-R=-2.00,-18.00,228.00
Sensor2:.#A-R=-15.00,-42.00,297.00
Sensor3:.#A-R=-10.00,-14.00,227.00
Sensor1:.#A-R=-1.00,-21.00,229.00
Sensor2:.#A-R=-14.00,-45.00,297.00
...
...
..
.
.
and so on
I want to store the data which begins in "Sensor1:.#A-R=" in a matrix, (I mean only the number values)

採用された回答

Walter Roberson
Walter Roberson 2023 年 2 月 26 日
S = fileread(FILENAME);
S = regexprep(S, '^#A-R=', '', 'lineanchors');
data = cell2mat( textscan(S, '%f%f%f', 'delimiter', ',') );
  3 件のコメント
M
M 2023 年 2 月 26 日
I tried this and it didnt work
S = fileread(D1);
S = regexprep(S, 'Sensor1:.#A-R=', '', 'lineanchors');
data = cell2mat( textscan(S, '%f%f%f', 'delimiter', ',') );
Walter Roberson
Walter Roberson 2023 年 2 月 26 日
testdata = {
'Sensor1:.#A-R=-1.00,-20.00,226.00'
'Sensor2:.#A-R=-13.00,-43.00,297.00'
'Sensor3:.#A-R=-10.00,-11.00,216.00'
'Sensor1:.#A-R=-2.00,-18.00,224.00'
'Sensor2:.#A-R=-15.00,-41.00,298.00'
'Sensor3:.#A-R=-11.00,-11.00,220.00'
'Sensor1:.#A-R=-2.00,-17.00,227.00'
'Sensor2:.#A-R=-15.00,-42.00,298.00'
'Sensor3:.#A-R=-9.00,-12.00,225.00'
'Sensor1:.#A-R=-2.00,-18.00,228.00'
'Sensor2:.#A-R=-15.00,-42.00,297.00'
'Sensor3:.#A-R=-10.00,-14.00,227.00'
'Sensor1:.#A-R=-1.00,-21.00,229.00'
'Sensor2:.#A-R=-14.00,-45.00,297.00'};
D1 = tempname() + ".txt";
writelines(testdata, D1);
S = fileread(D1);
S = regexp(S, '(?<=^Sensor1:.#A-R=)[^\n]+$', 'match', 'lineanchors');
S = strjoin(S, '\n');
data = cell2mat( textscan(S, '%f%f%f', 'delimiter', ',') );
data
data = 5×3
-1 -20 226 -2 -18 224 -2 -17 227 -2 -18 228 -1 -21 229

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by