Clear Filters
Clear Filters

Read text file with blank lines as spacer

5 views (last 30 days)
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.

Accepted Answer

LeoAiE
LeoAiE on 7 May 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

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!