Read and format multiple text files line by line

11 ビュー (過去 30 日間)
vanrapa
vanrapa 2020 年 3 月 13 日
コメント済み: vanrapa 2020 年 3 月 14 日
Hi all,
I have about 50 text files. Each text file has 17 columns and several rows of data(attached a sample file).
Now I have to extract data from one file, plot and save a figure from the data, and close the file. I have to repeat this for all the text files. I have tried writing the following code to first open file, extract data and close file,
tfiles = dir('*.txt');
ntfiles = length(tfiles);
for i = 1 : ntfiles
fid = fopen(tfiles(i).name);
D = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f');
fclose(fid);
end
But my code displays only blank values. So how to extract all the lines?
Also I would like to know how to proceed with these issues,
1. I know how to remove the first 8 header lines. But the header seems to be repeated throughout the text. So is there any way I could look for a particular string and delete the entire row from that?
2. Also I need to delete rows which does not contain values in all the 17 columns or contains value in more than 17 columns. How can I check this condition?
Any help would be very useful.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 13 日
編集済み: Ameer Hamza 2020 年 3 月 13 日
Try this
fid = fopen('sample.txt', 'r');
data_array = {};
while ~feof(fid)
data = cell2mat(textscan(fid, repmat('%f ', 1, 17)));
if isempty(data)
fgetl(fid);
else
data_array{end+1} = data;
end
end
data_array = cell2mat(data_array');
mask = data_array(:,1) == 1;
data_array(~mask, :) = []; % only rows with first element 1 are part of actual dataset
You can extend it to multiple files in for loop.
  1 件のコメント
vanrapa
vanrapa 2020 年 3 月 14 日
It's neat, rather than checking for strings and column data. Thanks a lot.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by