Loop Files from a Directory

4 vues (au cours des 30 derniers jours)
Qingsheng Bai
Qingsheng Bai le 4 Sep 2017
Commenté : Qingsheng Bai le 4 Sep 2017
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 commentaires
Stephen23
Stephen23 le 4 Sep 2017
Modifié(e) : Stephen23 le 4 Sep 2017
@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 le 4 Sep 2017
Valid point Stephen. Edited my answer.

Connectez-vous pour commenter.

Réponse acceptée

KL
KL le 4 Sep 2017
Modifié(e) : KL le 4 Sep 2017
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');
  1 commentaire
Qingsheng Bai
Qingsheng Bai le 4 Sep 2017
That what I need. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by