Read and format multiple text files line by line

4 vues (au cours des 30 derniers jours)
vanrapa
vanrapa le 13 Mar 2020
Commenté : vanrapa le 14 Mar 2020
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.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 13 Mar 2020
Modifié(e) : Ameer Hamza le 13 Mar 2020
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 commentaire
vanrapa
vanrapa le 14 Mar 2020
It's neat, rather than checking for strings and column data. Thanks a lot.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by