필터 지우기
필터 지우기

Read data from a complex file

조회 수: 5 (최근 30일)
Research
Research 2023년 9월 26일
댓글: Voss 2023년 9월 28일
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

채택된 답변

Voss
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);
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
% 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')
T = 15×4 table
dt vals1 vals2 vals3 ___________ _____ _____ _____ 25-Apr-0016 5 6 7 25-Apr-0016 24 2 25 25-Apr-0016 52 62 72 25-Apr-0017 2 24 2 25-Apr-0018 52 62 72 25-Apr-0019 25 26 28 25-Apr-0019 52 62 72 25-Apr-0019 52 62 72 25-Apr-0020 2 24 2 25-Apr-0021 52 62 72 25-Apr-0022 25 26 28 25-Apr-0023 5 6 7 25-Apr-0024 24 2 25 25-Apr-0024 5 6 7 25-Apr-0025 24 2 25
  댓글 수: 2
Research
Research 2023년 9월 28일
Thank you. It is an elegant solution.
Voss
Voss 2023년 9월 28일
You're welcome! And thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by