Read data from a complex file
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi Help,
I'm trying to read the data from this kind of file. Here the headers repeat randomly at various rows and first column data is date of the year. I tried using load and readmatrix functions, but no luck. Any thoughts?
Data file has four columns. First column data is day (mm-dd-yy). Header labels from each row repeats across the file but not periodically.
header header2 header3 header4
4-25-16 5 6 7
4-25-16 24 2 25
header header2 header3 header4
4-25-16 52 62 72
4-25-17 2 24 2
4-25-18 52 62 72
4-25-19 25 26 28
header header2 header3 header4
4-25-19 52 62 72
header header2 header3 header4
4-25-19 52 62 72
4-25-20 2 24 2
4-25-21 52 62 72
4-25-22 25 26 28
4-25-23 5 6 7
4-25-24 24 2 25
header header2 header3 header4
4-25-24 5 6 7
4-25-25 24 2 25
댓글 수: 0
채택된 답변
Voss
2023년 9월 26일
편집: Voss
2023년 9월 26일
Something like the following may work. You may need to change the header_str and text_scan_format to match your actual file's contents.
% path to the file:
file_name = 'file.txt';
% lines in the file starting with header_str will be skipped:
header_str = 'header';
% format of data lines:
text_scan_format = '%d-%d-%d %d %d %d';
% show file's contents, for reference:
type(file_name);
% initialize an empty datetime array dt and an empty 3-column matrix vals:
dt = NaT(0,1);
vals = zeros(0,3);
% open the file for reading:
fid = fopen(file_name,'r');
% while not at the end of the file:
while ~feof(fid)
% get the next line:
str = fgetl(fid);
% if it starts with header_str, skip it:
if startsWith(str,header_str)
continue
end
% read the date and other values from the line:
temp = textscan(str,text_scan_format);
% put the date in a new elements in the dt array:
dt(end+1,:) = datetime(temp{[3 1 2]}); % year, month, day
% put the other values in a new row in the vals matrix:
vals(end+1,:) = [temp{4:6}];
end
% close the file:
fclose(fid);
% put dt and vals together in a table:
T = addvars(array2table(vals),dt,'Before','vals1')
추가 답변 (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!