Clear Filters
Clear Filters

Question on Reading lines from text file.

1 view (last 30 days)
Hi,
I have the following data in the txt file:
2 34 5 6 7 8
34 56 77 12 3
11 34 55 66 89 1
12 34 55 67 8 1
45 67 8 1 2 14
23 45 1 2 8 9
I want to read my file from third line to 5th line and save it in a matrix say A. That is A should be:
A = 11 34 55 66 89 1
12 34 55 67 8 1
45 67 8 1 2 14
I can do this if I have to read my lines from the start to a specified number of lines using fopen and fgetl ...however, I cant figure out how to do this for the above situation. Please Help!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 6 Dec 2014
fid=fopen('file.txt')
for k=1:2
fgetl(fid);
end
A=zeros(3,6)
for k=1:3
A(k,:)=str2num(fgetl(fid))
end
fclose(fid)

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!