read a text file or not read it

Hi I have a lot of text files.
%
file1.txt
file2.txt.
.
.
.
.
file100
i read this files with this function:
%
for k=1:100
fid=fopen(['file_' num2str(k) '.txt']);
if fid == -1, error('Cannot open file'),end
result{k,1}=textscan(fid,'%f %f %f %f %f');
fclose(fid)
end
i want to scip reading file50.txt and file69.txt. How could i do this with matlab. tell hem not to read these both files? Thank you

 채택된 답변

Jan
Jan 2012년 11월 7일

0 개 추천

Or if you do not want to hard code it:
result = cell(1, 100);
index = 0;
for k=1:100
fid = fopen(['file_' num2str(k) '.txt']);
if fid ~= -1
result{index, 1} = textscan(fid,'%f %f %f %f %f');
fclose(fid)
end
end
result = result(1:index); % Crop empty cells

추가 답변 (1개)

José-Luis
José-Luis 2012년 11월 7일
편집: José-Luis 2012년 11월 7일

0 개 추천

idx = 1:100;
idx([50 69]) = []; %You could also use an if statement inside the loop, but this might be more efficient
for k=idx
fid=fopen(['file_' num2str(k) '.txt']);
if fid == -1, error('Cannot open file'),end
result{k,1}=textscan(fid,'%f %f %f %f %f');
fclose(fid)
end

카테고리

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

질문:

2012년 11월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by