Read text file with blank lines as spacer
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jorge Luis Paredes Estacio
le 7 Mai 2023
Commenté : Jorge Luis Paredes Estacio
le 7 Mai 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 commentaires
Réponse acceptée
LeoAiE
le 7 Mai 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB 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!