필터 지우기
필터 지우기

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