Read .txt Files in Looping
조회 수: 13 (최근 30일)
이전 댓글 표시
I am working in a observation and the data is saved in .txt. I have 111 folders which each of them consists of 816 .txt files and this is how the data looks like.
And this is the script I created to read all files in a folder.
dir_in = 'D:\DATA 1\Materi & Tugas Kuliah\Tugas Akhir\c2\a\c020_out\';
files = dir([dir_in, '\*.txt']);
filename = (files.name);
fid = fopen(filename, 'rt');
c = textscan(fid,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s');
lat = c{4};
lon = c{5};
SSH = c{16};
SLA = c{17};
MJD = c{3};
YYF = c{19};
fclose(fid);
But everytime I run the script, the data does not appear. Textscan on variable 'c' reads the first line of the files ONLY, which is the header line, either using 'headerlines' option or not. I guess something wrong goes in that 'fid', but I am not really sure.
I hope someone can help me out. Thank you!
댓글 수: 0
채택된 답변
Walter Roberson
2019년 3월 23일
편집: Walter Roberson
2019년 3월 23일
filenames = fullfile(dir_in, {files.name});
numfiles = length(filenames);
lat = cell(numfiles,1);
lon = cell(numfiles,1);
SSH = cell(numfiles,1);
SLA = cell(numfiles,1);
MJD = cell(numfiles,1);
YYF = cell(numfiles,1);
numfields = 19; %adjust as needed
for K = 1 : numfiles
fid = fopen(filenames{K}, 'rt');
c = textscan(fid, repmat('%f', 1, numfields), 'Headerlines', 1);
lat{K} = c{4};
lon{K} = c{5};
SSH{K} = c{16};
SLA{K} = c{17};
MJD{K} = c{3};
YYF{K} = C{19};
fclose(fid);
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!