MATLAB Answers

0

Import data from txt file

Ferdinand Grosse-Dunker さんによって質問されました 2019 年 7 月 16 日
最新アクティビティ Adam Danz
さんによって コメントされました 2019 年 7 月 16 日
Hi there,
my txt file looks like this: (see attached txt file)
What I try to get is a simple table with lines like this:
frame; timestamp [s]; loc_x; loc_y; loc_z
1007; 52045.728; -135.966; 225.786; -52.285
1008; 52045.745; -135.966; 225.786; -52.285
How can I do this? Thanks

  3 件のコメント

Adam Danz
2019 年 7 月 16 日
I see the frame and timestamp components. Where are the xloc yloc etc.?
What are the numbers 1007; 52045.728; -135.966; 225.786; -52.285?
frame 1010 ts 52045.778 nbod 4 nfly 0 nmea 0 nmearef 0 nhand 0 nmar 0 nhuman 0 ninertial 4
bod 0 not tracked
bod 1 not tracked
bod 2 qu 1.000 loc -135.967 225.786 -52.283 rot -0.282 -0.960 0.001 0.960 -0.282 -0.008 0.008 -0.001 1.000
bod 3 not tracked
no human model data
inertial body 0 st 0 error 0.000
inertial body 1 st 0 error 0.000
inertial body 2 st 2 error 0.000
loc -135.967 225.786 -52.283 rot -0.282 -0.960 0.001 0.960 -0.282 -0.008 0.008 -0.001 1.000
inertial body 3 st 0 error 0.000
Rik
2019 年 7 月 16 日
Do you need code that only works on this file, or should it also handle multiple bodies being tracked?
What have you tried so far on your own?
In line 1 of each block is the frame number (1007) and the timestamp (52045.728 seconds)
In line 10 of each block is the location and rotation data. Starting with location (loc_x, loc_y, loc_z) then rotation (rot_xx, rot_xy, rot_xz; rot_yx, rot_yy, rot_yz; rot_zx, rot_zy, rot_zz).
All other lines of each block are not necessary.

サインイン to comment.

2 件の回答

Adam Danz
回答者: Adam Danz
2019 年 7 月 16 日
編集済み: Adam Danz
2019 年 7 月 16 日
 採用された回答

This uses readtable() to read in your txt file and it uses regexp() with the 'tokens' flag to pull out relevant data.
%% Import data into table
opts = delimitedTextImportOptions("NumVariables", 1);
opts.DataLines = [1, Inf];
opts.VariableNames = "txt";
test1 = readtable("test1.txt", opts); %file is on path
%% Extract desired data
% Pull out the frame and ts data
tokens = regexp(test1.txt, 'frame (\d+) ts (\d+.\d+)', 'tokens');
frame_ts = cell2mat(cellfun(@str2double,[tokens{:}]','UniformOutput',false));
% Pull out loc coordinates
tokens = regexp(test1.txt, '^loc (-?\d+.\d+) (-?\d+.\d+) (-?\d+.\d+)', 'tokens');
loc = cell2mat(cellfun(@str2double,[tokens{:}]','UniformOutput',false));
% Check that frame_ts and loc have same number of rows.
if size(frame_ts,1) ~= size(loc,1)
error('''frame_ts'' and ''loc'' have different number of rows.')
end
%% Put data into table
T = array2table([frame_ts, loc], 'VariableNames', {'frame', 'timestamp', 'loc_x', 'loc_y', 'loc_z'});
% inspect first few rows
head(T)
Result
format long
>> head(T)
ans =
8×5 table
frame timestamp loc_x loc_y loc_z
_____ _________ ________ _______ _______
1007 52045.728 -135.966 225.786 -52.285
1008 52045.745 -135.966 225.786 -52.285
1009 52045.761 -135.967 225.786 -52.283
1010 52045.778 -135.967 225.786 -52.283
1011 52045.795 -135.967 225.786 -52.283
1012 52045.811 -135.967 225.786 -52.283
1013 52045.828 -135.967 225.786 -52.283
1014 52045.845 -135.967 225.786 -52.283

  0 件のコメント

サインイン to comment.


回答者: Ferdinand Grosse-Dunker 2019 年 7 月 16 日

This works great! Thanks a lot Adam!

  1 件のコメント

Adam Danz
2019 年 7 月 16 日
Glad I could help!

サインイン to comment.



Translated by