Read and format multiple text files line by line
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!