While reading data from text file line by line using fget1, If error on data how to go to next line?

Hi, I am new to this form so sorry for any mistakes.
My question is what I am after. So I have set of data been read line by line using fget1, but my data can have sometimes lines or some data missing.
So what I need program to do is if there is empty line but not EOf, or if data missing within each line just fill with NaN or skip and go to next line.
example of my code:
try
tline = fgetl(fileID);
while (tline ~= -1)
if (strncmp(tline,'01',2))
%get data
Time_num=addtodate(Time_num,100,'millisecond');
Data = textscan(tline,'%*s%*s%*s%d16%d16%d16%d16%d16%d16%c%c%c%c%c%c\n'...
,'delimiter',',');
DATA(dataLine,:) = cell2mat(Data(1,1:6));
%V_A(dataLine,:)=Data(1,7:12);
Time_Axis(dataLine,:)=Time_num;
dataLine=dataLine+1;
end
end
catch err
errordlg('There has been an error with your Import File.',' File Error #2017');
end

3 comentarios

Please follow the "? Help" link to learn how to format code.
The code you posted reads only one line from a file and can result in an endless loop. I guess you miss an additional fgetl inside the while loop. And what kind of error do you get?
Yes, I read 1 line at time and process the data into already made matrix's, as my data can be different each line I need to know what line is that data and where to place it, The above code is only small part of my import script, If you do require rest of the code I can share.
What I need to do now error handling and check if my text file got empty line or the data is not as it meant to be instead of just giving error just go to next line.
Ex of my data can be:
03,03,00,DEV,29/08/13,06:18:14,STOP
02,03,00,$GPRMC,061812,A,5331.7518,N,00232.6979,W,000.1,322.4,290813,002.
01,03,00, 6101, 0, 3, 3, -2, -9,V,V,V,V,V,V
01,03,00, 64, 0, 3, 3, -2, -9,V,V,V,V,V,V
01,03,00, 64, 1, 3, 3, -3, -8,V,V,V,V,V,V
01,03,00, , , , , , ,V,V,V,V,V,V
01,03,00, 61, 0, 5, 3, -3, -10,V,V,V,V,V,V
01,03,00, 61, 0, 4, 4, -2, -10,V,V,V,V,V,V
03,03,00,GPA,29/08/13,06:18:15,
As you can see there is empty line within my data which the fget1 will stop at, and also some data missing. I want be enable to handle this by just skiping line or filling them with NaN.
thanks in advance

Iniciar sesión para comentar.

 Respuesta aceptada

It would be up to your line processing code to not generate an error on an malformed line. I would write code something like this:
while ~feof(fileID)
tLine = fgetl(fileID);
% Process line here (and don't generate an erro)
end
The feof/fgetl function are called each loop iteration, regardless of the success or failure of the previous line's processing.

2 comentarios

Remember to test ischar(tLine) before using it, as feof() only tells you whether a read operation already encountered end-of-file, not that a read operation would encounter end-of-file.
Thais Great tip, Thanks I got the skipping space and empty line error sort it using the while Not EOF and also Walter tip to check for char but I still can not get line to be ignored if the data has something that not meant to have.
So if my data lines should only have integer within the device that generate this data can have fault and add letter or ASCII character within data so I would like to skip that line of data with my import script.
Line data with character instead of integer which should be ignored:
01,03,00, 64, a, 3, 3, -2, -9,V,V,V,V,V,V

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import from MATLAB en Centro de ayuda y File Exchange.

Productos

Preguntada:

Ash
el 29 de Ag. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by