Read and format multiple text files line by line
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
vanrapa
am 13 Mär. 2020
Kommentiert: vanrapa
am 14 Mär. 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.
0 Kommentare
Akzeptierte Antwort
Ameer Hamza
am 13 Mär. 2020
Bearbeitet: Ameer Hamza
am 13 Mär. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!