Clear Filters
Clear Filters

Read txt data with blank rows

4 views (last 30 days)
Hello, I have some issues with the format of some earthquake signals. I realized that the acceleration data (after "Z N E" as shown in the attached text files) presents blank rows which does not allow me to collect the data. As you can see in the function below, when MATLAB reads the filename "20190912_095704_MOQA.txt" as an example it provides and issue. This is because the line 55 "tmp=sscanf(tline,'%f %f %f %f');" does not get the data as tmp and then it shows an issue with the next line "tmp1 = [tmp(3); tmp(2); tmp(1)]; % rearrange to E=X N=Y Z=Z". Could you please help me to sort the issue out. I also attached the text file as an example. The MATLAB script function is provided below. Thank you
function [fs, Output, STATION] = import_from_IGP4(filename)
% Output = [Time NS EO V]
% filename is a string; example: filename = 'SGC2021mvinun_BOG_11.anc'
% textline1 is a string; example: textline1= ' Z N E'
% textline2 is a string; example: textline2= ' Z E N'
% textline3 is a string; example: textline3= ' N Z E'
% textline4 is a string; example: textline4= ' N E Z'
% textline5 is a string; example: textline1= ' E Z N'
% textline6 is a string; example: textline1= ' E N Z'
textline1 = ' Z N E';
textline2 = ' Z E N';
textline3 = ' N Z E';
textline4 = ' N E Z';
textline5 = ' E Z N';
textline6 = ' E N Z';
fid = fopen(filename,'r');
tline = fgetl(fid);
i = 1;
Output = [];
index = 0;
index_fs = 0;
index_station = 0; %index added for stations
while ischar(tline)
%new condition added for stations
if index_station == 0
if strfind(tline,'CODIGO : ') > 0
index_station = 1;
STACODE = extractAfter(tline,"CODIGO : ");
STATION = STACODE(find(~isspace(STACODE)));
%index_braket = strfind(tline,"("); %include character until where it should be considered the name of the station
%STATION = tline(length('CODIGO : ')+1 : index_braket-2);
end
end
%find sampling frequency
if length(tline)>29 %%COUNT NUMBER OF CHARACTERS AND CHANGE IT AFTER >%%%%%%
index_fs = strcmp(tline(1:29),'MUESTREO : ');
if index_fs == 1
str_output = remove_letters_1(tline);
fs = str2double(str_output);
index_fs = 0;
end
end
%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
%Second mixed data%
if index==0
index = strcmp(tline,textline2); %% Z E N
if index ==1; index=2; end
elseif index==2
tmp=sscanf(tline,'%f %f %f %f');
tmp2 = [tmp(2); tmp(3); tmp(1)]; % rearrange to E=X N=Y Z=Z
Output = [Output; tmp2'];
end
%Third mixed data%
if index==0
index = strcmp(tline,textline3); %% N Z E
if index ==1; index=3; end
elseif index==3
tmp=sscanf(tline,'%f %f %f %f');
tmp3 = [tmp(3); tmp(1); tmp(2)]; % rearrange to E=X N=Y Z=Z
Output = [Output; tmp3'];
end
%Fourth mixed data%
if index==0
index = strcmp(tline,textline4); % N E Z
if index ==1; index=4; end
elseif index==4
tmp=sscanf(tline,'%f %f %f %f');
tmp4 = [tmp(2); tmp(1); tmp(3)]; % rearrange to E=X N=Y Z=Z
Output = [Output; tmp4'];
end
%Fith mixed data%
if index==0
index = strcmp(tline,textline5); % E Z N
if index ==1; index=5; end
elseif index==5
tmp=sscanf(tline,'%f %f %f %f');
tmp5 = [tmp(1); tmp(3); tmp(2)]; % rearrange to E=X N=Y Z=Z
Output = [Output; tmp5'];
end
%Sixth mixed data%
if index==0
index = strcmp(tline,textline6); % E N Z
if index ==1; index=6; end
elseif index==6
tmp=sscanf(tline,'%f %f %f %f');
tmp6 = [tmp(1); tmp(2); tmp(3)]; % rearrange to E=X N=Y Z=Z
Output = [Output; tmp6'];
end
tline = fgetl(fid);
i = i+1;
end
fclose(fid);
end
function str_output = remove_letters_1(str_input)
deleteMe = isletter(str_input); % Detect which ones are letters
str_input(deleteMe) = []; % Delete the letters
str_output = strip(str_input);
index = regexp(str_output, '[ :/ # ()[] ]');
str_output(index)=[];
end

Accepted Answer

Image Analyst
Image Analyst on 10 Jun 2023
Just check for null after you get the string and skip to the bottom of the loop if it's empty
while ischar(tline)
if isempty(strtrim(tline))
% It got a blank line. Need to skip it, otherwise errors occur later.
% Read the next line after this one to get ready for the next
% iteration of the loop.
tline = fgetl(fid);
continue; % Skip to bottom of loop and continue.
end
% Rest of loop goes here.
end
  3 Comments
Image Analyst
Image Analyst on 11 Jun 2023
I just pasted my code into your original code and it worked. Working code is attached.
Jorge Luis Paredes Estacio
Thank you very much. It works perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands 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!