how to know the format of the text file

1 回表示 (過去 30 日間)
pruth
pruth 2018 年 5 月 14 日
コメント済み: Walter Roberson 2018 年 5 月 15 日
hi, I have multiple text files in one folder. I want to read all the files and make one mat file from it.therefore it will append all the data from text files into one file. the main problem I face here is to read the text data, I am not able to identify the format of this file.i have attached 2 two files to get the idea about data. i want to read all files one by one, extract the data and create only one mat file. I hope you understand my question.
  3 件のコメント
pruth
pruth 2018 年 5 月 15 日
i am using matlab 2015a
Ameer Hamza
Ameer Hamza 2018 年 5 月 15 日
@pruth refer to my updated answer for code working in R2015a.

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

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 5 月 14 日
編集済み: Ameer Hamza 2018 年 5 月 15 日
This will append the content of these three files in the form of a table.
opts = detectImportOptions('RD-180315-103300.txt');
t1 = readtable('RD-180315-103300.txt', opts);
t2 = readtable('RD-180316-103300.txt', opts);
t3 = readtable('RD-180317-103300.txt', opts);
t = [t1; t2; t3];
The following code will work for the version between R2013b to R2016a.
f = fopen('RD-180315-103300.txt');
data1 = textscan(f, ['%{YYYY-MM-DD}D%{hh:mm:ss}T' repmat('%f', 1, 24)], 'HeaderLines', 1);
fclose(f);
f = fopen('RD-180316-103300.txt');
data2 = textscan(f, ['%{YYYY-MM-DD}D%{hh:mm:ss}T' repmat('%f', 1, 24)], 'HeaderLines', 1);
fclose(f);
f = fopen('RD-180317-103300.txt');
data3 = textscan(f, ['%{YYYY-MM-DD}D%{hh:mm:ss}T' repmat('%f', 1, 24)], 'HeaderLines', 1);
fclose(f);
data = [table(data1{:}); table(data2{:}); table(data3{:})];
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 5 月 15 日
%D formats were not accepted in R2013b or R2014a.
%T formats are new in R2018a.
So from R2014b to R2017b, if you do not use readtable, then use
data1 = textscan(f, ['%{YYYY-MM-DD}D%{hh:mm:ss}D' repmat('%f', 1, 24)], 'HeaderLines', 1, 'Delimiter', '\t');
data1{1} = data1{1} + (data1{2} - dateshift(data1{2}, 'start', 'day'));
data1(2) = [];

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by