Unable to read completely a text file

I have the following text file at the bottom, where every line ends with a semicolon. I am unable to read the entire file with these commands. I only obtain the content of the first line. Any help in reading the entire file would be greatly appreciated.
Thanks.
fileID = fopen(filename);
data = textscan(fileID, '%d %f %d %d %d', 'EndOfLine', ';')
fclose(fileID);
"filename.txt"
37 0.033 119 131 7;
38 0.034 138 138 8;
39 0.035 138 144 9;
40 0.035 138 150 10;
41 0.036 157 157 11;
42 0.037 157 163 12;
43 0.038 157 169 13;
44 0.039 176 176 14;
45 0.040 176 182 15;
46 0.041 176 189 17;
47 0.042 195 195 18;
48 0.043 195 202 20;
49 0.043 195 208 21;
50 0.044 215 215 23;

 Accepted Answer

Try something like this instead —
data = textscan(fileID, '%d %f %d %d %d;')
The format string should then read it correctly.
format shortG
FirstLine = '37 0.033 119 131 7;';
data = textscan(FirstLine, '%f %f %f %f %f;', 'CollectOutput',1)
data = 1×1 cell array
{[37 0.033 119 131 7]}
datam = cell2mat(data)
datam = 1×5
37 0.033 119 131 7
I made them all '%f' so that cell2mat would work with them.
.

4 Comments

This worked, thank you!
However, I was wondering why the option
'EndOfLine', ';'
wouldn't work.
As always, my pleasure!
It wouldn’t work because a semicolon is not an end-of-line operator. Those are almost always either '\n' or '\r\n'. Other combinations may exist, however I’m not aware of them.
OK, thank you.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Asked:

on 9 Apr 2022

Commented:

on 10 Apr 2022

Community Treasure Hunt

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

Start Hunting!