Read text file with blank lines as spacer
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jorge Luis Paredes Estacio
el 7 de Mayo de 2023
Comentada: Jorge Luis Paredes Estacio
el 7 de Mayo de 2023
Hello, I have an issue with some records. I want to get the data after " ' Z N E'" which can contains more than 20000 rows separated by three columns. However, these data for some cases are separated by blank lines (See example 2). The script I am using is the following for the case no blank lines are encountered (See Example1)
textline1 = ' Z N E';
%First mixed data%
if index==0
index = strcmp(tline,textline1); %%Z N E
if index ==1; index=1; end
elseif index ==1
tmp=sscanf(tline,'%f %f %f %f');
tmp1 = [tmp(3); tmp(2); tmp(1)]; % rearrange to E=X N=Y Z=Z
Output = [Output; tmp1'];
end
This works for a text file in this form (example 1)
Z N E
0.011534 0.053870 0.053166
-0.010678 -0.022890 0.002420
-0.005944 -0.012439 0.011069
However, there are plenty of files that has this format (example 2)
Z N E
0.011534 0.053870 0.053166
-0.010678 -0.022890 0.002420
-0.005944 -0.012439 0.011069
That is why it generates and error. Any help please to modify the coding for this case. Thank you very much.
0 comentarios
Respuesta aceptada
LeoAiE
el 7 de Mayo de 2023
You can add a condition to check if the current line is not empty before processing it. This should handle the case where you have blank lines in the input file.
textline1 = ' Z N E';
% First mixed data%
if index==0
index = strcmp(tline, textline1); %%Z N E
if index == 1; index = 1; end
elseif index == 1
% Check if the current line is not empty
if ~isempty(strtrim(tline))
tmp = sscanf(tline, '%f %f %f %f');
tmp1 = [tmp(3); tmp(2); tmp(1)]; % rearrange to E=X N=Y Z=Z
Output = [Output; tmp1'];
end
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Import and Export en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!