How to run the same code with many files (different file name in the same directory) in Matlab?

I have a thousand .dat files to work with the same program. Is there a faster way or script to run it automatically and not run them one by one? .Dat files have different file names.

The program looks something like this:

fid=fopen('**abd**.dat');
C=textscan(...);
...
save('**abd**.txt',data);

Abd is the name of the file. I have thousands of files with different file names. This is a little annoying, keep copying and pasting these file names into the program and running it. Has anyone got a faster way or code for this?

+5
source share
2 answers

"dir", , .

fns = dir('*.dat');
for i = 1:length(fns)
    fid = fopen(fns(i).name);
    C = textscan(...);
    fclose(fid);
    save([fns(i).name,'.dat'],data);

end
+6

. script . 2 , .

+1

All Articles