Read text file with multiple delimiters in a single row
2 views (last 30 days)
Show older comments
Daniel Rowe
on 25 Jan 2022
Answered: Walter Roberson
on 26 Jan 2022
Hi
I have some accelerometer data from an Arduino board (see attached test file showing 6 columns of data but with multiple delimiters on each row (commas and \t)). Each row of data (fron 2nd row onwards) features a high precision time stamp, type, arduino time, X, Y and Z components). I'm not sure how how to separate the data into individual columns using textscan.
2nd line of data:
1643148827196364700,0.92554\t0.34692\t0.15991 translates to: 16:43:14.882 (time stamp) 7196364700 (type and arduino time - honestly I'm not sure what the split is here) 0.92554 9 (X) 0.34692 (Y) 0.15991 (Z)
Can someone please help?
data_dir = '/MATLAB Drive/Arduino Accel';
test_id = 'Test';
path = fullfile(data_dir,strcat(test_id,'.txt'));
fid = fopen(path, 'rt');
tline = fgetl(fid);
headers = strsplit(tline, ',');
%% datacell = textscan(fid, ....???
% fclose(fid);
0 Comments
Accepted Answer
Walter Roberson
on 26 Jan 2022
Your timestamps are too low precision for it to be reasonable that you stop at milliseconds -- you have too many values that are the same to within the millisecond. It makes more sense if you assume that more digits are allocated to time. When I look at the plots, I cannot really tell the difference between 3 extra or 4 extra digits; 3 seems more common so I coded for that.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/873970/Test.txt';
S = urlread(filename);
data = textscan(S, '%2f%2f%2f%3f%3f%7u,%f\\t%f\\t%f', 'HeaderLines', 1)
Ts = duration(data{1}, data{2}, data{3}, data{4} + data{5}/1E3);
TaAT = data{6};
X = data{7};
Y = data{8};
Z = data{9};
figure
plot(Ts); title('time')
figure
plot(Ts-Ts(1), [X-mean(X), Y-mean(Y), Z-mean(Z)]); legend({'X', 'Y', 'Z'});
format long g
seconds(Ts(end)-Ts(1))
figure
plot3(X, Y, Z); title('X Y Z'); xlabel('x'); ylabel('y'); zlabel('z')
0 Comments
More Answers (0)
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!