Clear Filters
Clear Filters

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

10 views (last 30 days)
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)

Accepted Answer

Walter Roberson
Walter Roberson on 26 Feb 2023
S = fileread(FILENAME);
S = regexprep(S, '^#A-R=', '', 'lineanchors');
data = cell2mat( textscan(S, '%f%f%f', 'delimiter', ',') );
  3 Comments
M
M on 26 Feb 2023
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 on 26 Feb 2023
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

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!