Clear Filters
Clear Filters

How to delete the header of the 1st line of .txt

6 views (last 30 days)
Hello, I would like to ask a question. I have multiple .txt files that I want to merge in vertical manner. the format of these .txt files is the attached file
(1 header & 1 line with numbers)
I am using the commands:
clc
clear
filename1 = ['19*.txt'];%filename
fid_t=fopen(filename1,'r')
fid_p = fopen('final3.txt','w'); % writing file id
% if fid_p < 0, error('Cannot open file for writing.'); end
x = dir ('19*.txt');
for i = 1:length(x)
filename1 = x(i).name; %filename
fid_t = fopen(filename1, 'r');%open it and pass id to fscanf (reading file id)
if fid_t < 0, error('Cannot open file for reading.'); end
data = fread(fid_t, inf, '*char');%read data
fwrite(fid_p, data, 'char');%print data in File_all
fclose(fid_t);% close reading file id
end
but the point is that I would like not to take into account the headers of all the .txt files.
How can I make it?

Accepted Answer

Karim
Karim on 23 Feb 2022
You could try it via the read and write table functions
% set the number of variables
opts = delimitedTextImportOptions("NumVariables", 9);
% start from second row
opts.DataLines = [2, Inf];
% use tab as delimiter
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["DATE_EE", "SIZE", "LATITUDE", "LONGTITUDE", "LAT", "LON", "COUNT", "MAX_VALUE", "MIN_VALUE"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% read all files
AllFiles = dir('19*.txt');
for i = 1:length(AllFiles)
tmpData = readtable(AllFiles(i).name, opts);
if i == 1
AllData = tmpData;
else
AllData = vertcat(AllData,tmpData);
end
end
% write output file
writetable(AllData, "final.txt",'Delimiter','\t')
  4 Comments
Ivan Mich
Ivan Mich on 23 Feb 2022
Yes I want to use different headers. I am uploading one input and the final .txt
Karim
Karim on 23 Feb 2022
ok the code i provided was based using the assumption of the header you showed in you initial example text file. If you want to use different headers you need to modife the
opts.VariableNames
variable in the routine so that it contains the correct variables.
for your new example this would be
opts.VariableNames = ["DATE_EARTHUAKE","Magnitude","lat_epicenter","lon_epicenter","macroseismic_lat","macroseismic_lon","Distance_Between_Epicenters","Macroseismic_Magnitude_IDPS_lt8","Macroseismic_magnitude_allIDPS"];
Alternatively, you can find some code below that doesn't look at the headers. It just skips the first line and merges the rest of the lines into one big file. However, if you use this, keep in mind that this code does not look at the order or names in the header. If your different files use a different ordering they will be concateiate wrongly.
% read all files
AllFiles = dir('19*.txt');
AllData = char('');
for i = 1:length(AllFiles)
tmpData = fileread(AllFiles(i).name);
strtIdx = regexp(tmpData,'\n');
AllData = [AllData tmpData(strtIdx(1)+1:end-1)];
end
fid_out = fopen("final.txt",'wt+');
fprintf(fid_out, '%s',AllData);
fclose(fid_out);

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!