Read text file with blank lines as spacer
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!