How to iterate through several files

I have several data files, data0.txt, data1.txt, etc. that contain numbers separated by spaces. I would like to iterate through each of these files and run some calculations:
file_strs = {'D:\Data\data0.txt' 'D:\Data\data1.txt' 'D:\Data\data2.txt'};
for a = 1:length(file_strs)
T1=importdata(file_strs(a), ' ');
% Do some calculations
......
end;
However, when I try this, I get:
Error using importdata (line 137)
Unable to open file.
Error in test (line 20)
T1=importdata(file_strs(a), ' ');
What am I doing wrong?
Steve

댓글 수: 2

Ryan
Ryan 2012년 7월 30일
편집: Ryan 2012년 7월 30일
Are you sure that the data file name exists exactly as written in the exact location given? Is another program busy using the file?
Yash
Yash 2012년 7월 30일
why not convert them into excel?

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2012년 7월 30일

0 개 추천

Did you use exist(filename, 'file') to check that it exists before you use it? Try that.
Also, whenever you're using cell arrays and something doesn't work try switching parentheses to braces () -> {}, or vice versa {} -> (). That's a good rule of thumb. Like T1=importdata(file_strs{a}, ' '); Try that also.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by