How to read in a .txt and arrange the data into a scheme?

1 回表示 (過去 30 日間)
Landor Viktor
Landor Viktor 2021 年 7 月 13 日
回答済み: Mathieu NOE 2021 年 7 月 13 日
Hi, I have some problem with reading in a txt file, I have to read in a txt file with about 7000 records, 1 headerline. After reading with textscan how can I arrange the datas into arrays or sg like that to do some postprocess and ploting with them?
fid = fopen(filename,'r');
cdata = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f','Delimiter','\t','headerLines',1);
Thanks

回答 (2 件)

Simon Chan
Simon Chan 2021 年 7 月 13 日
I try to use readcell as follows:
rawdata=readcell('test1.txt');
header = rawdata(1,:);
data = cell2mat(rawdata(2:end,:));
tiledlayout(5,6);
for k = 1:27
nexttile
plot(data(:,1),data(:,k+1));
ylabel(header{k+1});
xlabel(header{1});
end
  2 件のコメント
Landor Viktor
Landor Viktor 2021 年 7 月 13 日
Thank you, can you please help me, if I want to plot for example accelerations on one graph with other colors? e.g acc_x with red, acc_y with blue and acc_z with green and for magneto and gyro too. Thanks
Simon Chan
Simon Chan 2021 年 7 月 13 日
I am not sure which gyro you want, but you can modify the code yourself to suit your purpose:
figure
plot(data(:,1),data(:,11),'r',data(:,1),data(:,12),'b',data(:,1),data(:,13),'g')
legend(header{11:13})
xlabel(header{1});

サインインしてコメントする。


Mathieu NOE
Mathieu NOE 2021 年 7 月 13 日
hello
this is the best solution (IMO) : it will generate a table with identified variables names you can easily access :
T= readtable('test1.txt');
% if we want to plot data (example)
time = T.SampleTime_s_;
Gyro_1_X_dps_ = T.Gyro_1_X_dps_;
plot(time,Gyro_1_X_dps_); % and so forth...

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by