Extracting Data from file with varying columns

1 回表示 (過去 30 日間)
Ravindra
Ravindra 2018 年 5 月 22 日
コメント済み: Ameer Hamza 2018 年 5 月 22 日
Hi all, Our system is capturing data continuously. Where first 14 columns are always stored in *.txt and *.log file. The 15th column is added only when the image is captured. So the data in file looks like following:
I am asking this question, because I was not successful after trying out importdata, readtable and all possible MATLAB answers. When the pointer comes to the 15th column data. It reads the value for e.g. (here 11) and store it to the next row instead in the same row. Also, the values read after that all are NaN.
It would great, If I am able to load the this data automatically. Using a self written function.
Thank you very much for the help and valuable suggestions in advance.
  3 件のコメント
jonas
jonas 2018 年 5 月 22 日
did you try dlmread?
Ravindra
Ravindra 2018 年 5 月 22 日
small part of the data is following

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

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 5 月 22 日
編集済み: Ameer Hamza 2018 年 5 月 22 日
Try something like this
f = fopen('data.txt');
data = textscan(f, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f', 'Headerlines', 1); %%read 15 columns
fclose(f);
data{end} = [data{end}; nan(length(data{1})-length(data{end}))]; %%make length of last column equal to other columns
result = table(data{:})
result will contain the data in the form of table.
Edit: Edited the answer according to the uploaded data file.

その他の回答 (1 件)

Ravindra
Ravindra 2018 年 5 月 22 日
I modified the line because of header in first row
data = textscan(f, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','headerLines', 1);
and it worked thanks!!
  1 件のコメント
Ameer Hamza
Ameer Hamza 2018 年 5 月 22 日
Yes, I noted that after you uploaded the file and modified the answer.

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

カテゴリ

Help Center および File ExchangeString についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by