Find end of line (EoL) in a text file using MATLAB code

hi, how test end of line (EoL)?
thanks

1 Comment

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.

Sign in to comment.

Answers (3)

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 Comments

@Walter Roberson: My apologies, I totally misread the post as EOF..ugh. I deleted my response.
"Newline" can mean LineFeed: CHAR(10) or CarriageReturn: CHAR(13) or under Windows the combination CHAR[13, 10].
I read from txt file under windows.
How test?
I read my file in z
while z~=char(10)
this did not work.
thanks
please how test end of line?
I know char(10)
I have to use while, right?
if so, with what?
thanks
@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?
how I know end of line in file.
I wonder, is not there way to test EOL?
my file contains rows with different number of columns .
So, need test end of line
my file:
712 9511 746 6 6250 10871 9635 7811
152 11344 7608 10394 9635 9805 10369 649 3824 9904 10910 1
10046 10742 10551 9639 10394 10
10377 10908 11004 10418 10773 10448 10542 1607 2586 10449 10746
i want to say, I don't need fgetl.
thanks
Why do you say you don't need fgetl() when that seems to be the solution to your problem?
because fgetl return for example:
c=fgetl('f.txt')
the first line:
c=712 9511 746 6 6250 10871 9635 7811
I face difficult when I store it in matrix
where c(1)=7 but not 712
thanks

Sign in to comment.

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
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 Comment

thank u very much .
the last code u sent is good.
look I found another command is very good and quick
data=dlmread('exp.txt')

Sign in to comment.

Categories

Find more on Scripts in Help Center and File Exchange

Tags

Asked:

on 10 Nov 2011

Edited:

on 28 Mar 2023

Community Treasure Hunt

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

Start Hunting!