Read and format multiple text files line by line
5 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Ameer Hamza
on 13 Mar 2020
Edited: Ameer Hamza
on 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.
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!