Read text file with multiple delimiters in a single row

5 ビュー (過去 30 日間)
Daniel Rowe
Daniel Rowe 2022 年 1 月 25 日
回答済み: Walter Roberson 2022 年 1 月 26 日
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);

採用された回答

Walter Roberson
Walter Roberson 2022 年 1 月 26 日
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)
data = 1×9 cell array
{730×1 double} {730×1 double} {730×1 double} {730×1 double} {730×1 double} {730×1 uint32} {730×1 double} {730×1 double} {730×1 double}
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))
ans =
0.000501000002026558
figure
plot3(X, Y, Z); title('X Y Z'); xlabel('x'); ylabel('y'); zlabel('z')

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by