Read text file with blank lines as spacer

2 ビュー (過去 30 日間)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 5 月 7 日
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.

採用された回答

LeoAiE
LeoAiE 2023 年 5 月 7 日
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
  1 件のコメント
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 5 月 7 日
Thank you very much!. It works perfectly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by