how to add header for new output file?

10 views (last 30 days)
greeting matlab expert, I have multiple meteorological data in txt format and the content just like below
2.11 METEOROLOGICAL DATA RINEX VERSION / TYPE
teqc 2018Jun8 20180904 15:17:08UTCPGM / RUN BY / DATE
Linux 2.6.32-573.12.1.x86_64|x86_64|gcc -static|Linux 64|=+ COMMENT
cjem MARKER NAME
7 PR TD HR WS WD RI HI # / TYPES OF OBSERV
0.0 PR SENSOR MOD/TYPE/ACC
0.0 TD SENSOR MOD/TYPE/ACC
0.0 HR SENSOR MOD/TYPE/ACC
0.0 WS SENSOR MOD/TYPE/ACC
0.0 WD SENSOR MOD/TYPE/ACC
0.0 RI SENSOR MOD/TYPE/ACC
0.0 HI SENSOR MOD/TYPE/ACC
0.0000 0.0000 0.0000 0.0000 PR SENSOR POS XYZ/H
END OF HEADER
18 4 25 2 57 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
18 4 25 2 57 30 1001.6 29.9 62.1 0.0 0.0 0.0 0.0
18 4 25 2 58 0 1001.6 30.0 62.2 0.0 0.0 0.0 0.0
18 4 25 2 58 30 1001.6 30.0 61.5 0.0 0.0 0.0 0.0
18 4 25 2 59 0 1001.6 30.0 61.4 0.0 0.0 0.0 0.0
18 4 25 2 59 30 1001.5 30.0 61.9 0.0 0.0 0.0 0.0
18 4 25 3 0 0 1001.5 30.0 61.9 0.0 0.0 0.0 0.0
The first six column below header are date time and I try to convert it to modified juliandate. This is my script
clc;clear; close all;
input_folder = 'E:\Data\data';
files = dir(fullfile(input_folder, '*.txt'));
file_paths = fullfile({files.folder}, {files.name});
nfile = numel(file_paths);
for i = 1 : nfile
data= readmatrix(file_paths{i},'NumHeaderLines',15);% a display of data in a single file
data(:,end)=[];
data(:,1) = data(:,1)+2000; %add year
T1 = array2table(data(:,7:end)); %main data
DT = datetime(data(:,1:6)); %datetime
jd = juliandate(DT); %Juliandate
MJD = jd - 2400000.5; %Modified Juliandate
T1 = addvars(T1,MJD,'before','Var1');
% % TT1 = table2timetable(T1);
% % TT1rt = retime(TT1,'daily','mean');
% % TT2 = timetable2table (TT1rt);
t=T1(:,:);
c=table2cell(t);
newdata = [c];
writecell (newdata,file_paths{i},'Delimiter','space');
end
how can I write new converted txt file with the header on?

Accepted Answer

Jan
Jan on 22 Jan 2023
Moved: Jan on 22 Jan 2023
You want to copy the header? Read the first 15 lines and write it:
[fid, msg] = fopen(file_paths{i}, 'r');
assert(fid > 0, msg);
Head = cell(1, 15);
for k = 1:15
Head{k} = fgetl(fid);
end
fclose;
[fid, msg] = fopen(theNexFile, 'w');
assert(fid > 0, msg);
fprintf(fid, '%s\n', Head{:});
Use 'WriteMode', 'append' in writecell afterwards.
Note: newdata = [c] is exactly the same as: newdata = c. [] is Matlab's operator for concatenation. [c] concatenates c with nothing.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!