Effacer les filtres
Effacer les filtres

Read data from a complex file

5 vues (au cours des 30 derniers jours)
Research
Research le 26 Sep 2023
Commenté : Voss le 28 Sep 2023
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

Réponse acceptée

Voss
Voss le 26 Sep 2023
Modifié(e) : Voss le 26 Sep 2023
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 commentaires
Research
Research le 28 Sep 2023
Thank you. It is an elegant solution.
Voss
Voss le 28 Sep 2023
You're welcome! And thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by