Find end of line (EoL) in a text file using MATLAB code
Mostrar comentarios más antiguos
hi, how test end of line (EoL)?
thanks
1 comentario
Jan
el 11 de Nov. de 2011
It would be helpful, if you explain the input. It matters, if you are reading a text file under Windows or getting data from a serial port under Linux.
Respuestas (3)
Walter Roberson
el 10 de Nov. de 2011
0 votos
If you have the file open in text mode, then end of line occurs when the character you read in is newline, char(10), sprintf('\n')
If you do not have the file open in text mode, then end of line is whatever that particular binary format says end of line is.
8 comentarios
Wayne King
el 11 de Nov. de 2011
@Walter Roberson: My apologies, I totally misread the post as EOF..ugh. I deleted my response.
Jan
el 11 de Nov. de 2011
"Newline" can mean LineFeed: CHAR(10) or CarriageReturn: CHAR(13) or under Windows the combination CHAR[13, 10].
huda nawaf
el 11 de Nov. de 2011
huda nawaf
el 11 de Nov. de 2011
Jan
el 11 de Nov. de 2011
@Huda: What is "z" in your example "while z~=char(10)"?
It would be much easier to help you, if you explain the problem more accurately. *What* do you want to compare with a line break?
huda nawaf
el 11 de Nov. de 2011
Image Analyst
el 11 de Nov. de 2011
Why do you say you don't need fgetl() when that seems to be the solution to your problem?
huda nawaf
el 11 de Nov. de 2011
Image Analyst
el 11 de Nov. de 2011
0 votos
You may not need to. Are you aware that you can read to the end of a line with built in functions fgets() and fgetl()?
fgets: Read line from file, keeping newline characters
fgetl: Read line from file, removing newline characters
Dr. Seis
el 11 de Nov. de 2011
It won't work if you read in each string one-by-one, you have to read in each character one-by-one for it to work.
fid = fopen('filename.txt','r');
char_temp = fscanf(fid,'%c',1);
while ~isequal(char_temp,sprintf('\n'))
char_temp = fscanf(fid,'%c',1);
end
disp('Have reached end of line');
OR...
fid = fopen('filename.txt','r');
char_temp = fscanf(fid,'%c',1);
while ~isequal(char_temp,char(10))
char_temp = fscanf(fid,'%c',1);
end
disp('Have reached end of line');
Now that we know you are working with numbers...
fid = fopen('filename.txt','r');
while ~feof(fid)
temp_str = fgetl(fid);
c = sscanf(temp_str,'%d');
% do things with "c"
end
fclose(fid);
1 comentario
huda nawaf
el 11 de Nov. de 2011
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!