using readtable function except for last five lines from text file

9 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Simon Chan
Simon Chan el 14 de Ag. de 2021
Editada: Simon Chan el 14 de Ag. de 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 comentarios
Simon Chan
Simon Chan el 15 de Ag. de 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 el 15 de Ag. de 2021
Dear @Simon, thank you for your additional explanation.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by