looping a matrix from a text file

6 views (last 30 days)
german l
german l on 16 Apr 2013
i am creating a a program that will add the row values from a txt file, but i dont know how to move to the second, third, forth and fifth row.
ex this is what is in my text file
1 3 5.0
2 3 4.0
6 2 1.0
1 5 3.0
4 5 2.1
i can get the first row to work in my loop but how do i tell it to keep running it for the next rows

Answers (2)

Carlos on 16 Apr 2013
Is it necessary to use a loop?
So if I understand well you want to sum the rows, in this case if
A =
1.0000 3.0000 5.0000
2.0000 3.0000 4.0000
6.0000 2.0000 1.0000
1.0000 5.0000 3.0000
4.0000 5.0000 2.1000
You want this
9.0000 9.0000 9.0000 9.0000 11.1000.
To achieve this just do
ans =
9.0000 9.0000 9.0000 9.0000 11.1000

Yao Li
Yao Li on 16 Apr 2013
Is it a large text file or a small one. If it's a small size text file(eg. <10MB). Use importdata() to import data from the text file to matlab first. If the size of the text file is too large, you may try:
And you can add a while or for loop to run the 2nd command sevral times to move from line to line

Community Treasure Hunt

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

Start Hunting!