Hi, I want to loop files in a folder. I used the following method:
datadirs = dir('../Inputdata/URL2');
dircell = struct2cell(datadirs)' ;
filenames = dircell(:,1);
datafilename =strcat('../Inputdata/URL2/',filenames(3)); %just a try.
fid = fopen(datafilename);
It gives me an error message.
But when I used:
ff = '../Inputdata/URL2/2017032312_8952.txt'; %(The same as the "datafiename").
fid = fopen(ff);
It worked.
WHY!!!
How to fix the problem?

댓글 수: 3

KSSV
KSSV 2017년 9월 4일
What files you want to open in a folder? Do they have any extension?
Stephen23
Stephen23 2017년 9월 4일
편집: Stephen23 2017년 9월 4일
@Qingsheng Bai: instead of making up your own complicated and buggy syntax, you would be much better off simply copying the examples shown in the MATLAB documentation:
or from here:
And to build the path strings you should definitely use fullfile instead of strcat.
KL
KL 2017년 9월 4일
Valid point Stephen. Edited my answer.

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

 채택된 답변

KL
KL 2017년 9월 4일
편집: KL 2017년 9월 4일

1 개 추천

Change
datafilename =strcat('../Inputdata/URL2/',filenames(3));
to
datafilename =fullfile('../Inputdata/URL2',filenames{3});
and if you're only playing with txt files, then why not
datadirs = dir('../Inputdata/URL2/*.txt');

추가 답변 (0개)

카테고리

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

질문:

2017년 9월 4일

댓글:

2017년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by