How to read/write files inside a loop?

조회 수: 3 (최근 30일)
Kedharnath Sairam
Kedharnath Sairam 2012년 10월 12일
if true
% code
end
I am trying to read/write files inside a loop but I am not certain how it is done.
For example, I have a set of files (Sec1.dat, Sec2.dat ..... Sec30.dat). I want to create code where I can index the file name and process the data. My idea is :
n = 30
for i = 1 : n-1
Amat = dlmread ('Sec(i).dat', ' ' )
Bmat = dlmread ('Sec(i+1).dat', ' ')
Operation 1
Operation 2
.
.
.
dlmwrite ('X(i).dat',X,' ')
end
I am facing problems with the indexing of the file names. Any help will be greatly appreciated.
Thank you

답변 (2개)

Razvan
Razvan 2012년 10월 12일
편집: Razvan 2012년 10월 12일
Use
sprintf('Sec%d.dat', i)
instead of 'Sec(i).dat'. So something like this
Amat = dlmread (sprintf('Sec%d.dat', i), ' ' );
Bmat = dlmread (sprintf('Sec%d.dat', i+1), ' ');
...
dlmwrite (sprintf('X%d.dat',i),X,' ')

Walter Roberson
Walter Roberson 2012년 10월 12일

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by