How to read comma separated .txt file in matlab R2018a?
12 次查看(过去 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)
0 个评论
采纳的回答
Walter Roberson
2023-2-26
S = fileread(FILENAME);
S = regexprep(S, '^#A-R=', '', 'lineanchors');
data = cell2mat( textscan(S, '%f%f%f', 'delimiter', ',') );
3 个评论
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!