using readtable function except for last five lines from text file

9 vues (au cours des 30 derniers jours)
sermet OGUTCU
sermet OGUTCU le 14 Août 2021
Commenté : sermet OGUTCU le 15 Août 2021
for i=1:2
line_check{i,:} = regexp(fileread(full_file_name(i,:)),'\n','split');
end_of_header_line(i) = find(contains(line_check{i,:},'++Coordinate'));
end
for j=1:2
tCOD{j,:}=readtable(full_file_name(j,:),'FileType','text', ...
'headerlines',end_of_header_line(j),'readvariablenames',0);
end
The above codes worked with the attached file without the last five lines. If the last five lines does not deleted, the codes give "All lines of a text file must have the same number of delimiters" error. How I can use readtable without reading the the last five lines from text file?

Réponse acceptée

Simon Chan
Simon Chan le 14 Août 2021
Modifié(e) : Simon Chan le 14 Août 2021
Use your similar method to detect the last line of the valid data and add some import options as follows:
for i=1:2
line_check{i,:} = regexp(fileread(full_file_name(i,:)),'\n','split');
end_of_header_line(i) = find(contains(line_check{i,:},'++Coordinate'));
end_of_data_line(i) = find(contains(line_check{i,:},'--Coordinate')); % Added this line
end
% Add some import options
for j=1:2
opts = detectImportOptions(fileread(full_file_name(j,:));
opts.DataLines=[end_of_header_line(j)+1 end_of_data_line(j)-1]; % Valid data line
% Following two lines are optional, depends on what format you want
%opts.Delimiter={' '};
%opts.ConsecutiveDelimitersRule='join';
tCOD{j,:}=readtable(full_file_name(j,:),opts);
end
  2 commentaires
Simon Chan
Simon Chan le 15 Août 2021
Try the following, also find some typo in my previous code and hence revised as follows:
for i=1:2
line_check{i,:} = regexp(fileread(full_file_name(i,:)),'\n','split');
end_of_header_line(i) = find(contains(line_check{i,:},'++Coordinate'));
end_of_data_line(i) = find(contains(line_check{i,:},'--Coordinate')); % Added this line
end
% Add some import options
for j=1:2
opts = detectImportOptions(full_file_name(j,:)); % Revised this line
opts.DataLines=[end_of_header_line(j)+1 end_of_data_line(j)-1]; % Valid data line
opts.Delimiter={' '};
opts.LeadingDelimitersRule='ignore';
opts.ConsecutiveDelimitersRule='join';
tCOD{j,:}=readtable(full_file_name(j,:),opts);
xyz{j,:}=tCOD{j,:}(:,16:18);
end
The output is a 3 columns matrix with 86,388 rows, hope this is what you want.
The header has offset by 3 columns and hence the header name starts from 13 until 15.
sermet OGUTCU
sermet OGUTCU le 15 Août 2021
Dear @Simon, thank you for your additional explanation.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by