looping a matrix from a text file

1 次查看(过去 30 天)
german l
german l 2013-4-16
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

回答(2 个)

Carlos
Carlos 2013-4-16
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
sum(A.')
Yielding
ans =
9.0000 9.0000 9.0000 9.0000 11.1000

Yao Li
Yao Li 2013-4-16
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:
fid=fopen(fileToRead,'r');
tline=fgetl(fid);
And you can add a while or for loop to run the 2nd command sevral times to move from line to line

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by