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

18 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by