如何运行相同的code与Matlab中许多文件(在同一目录下不同的文件名)? [英] How to run the same code with many files (different file name in same directory) in Matlab?

查看:645
本文介绍了如何运行相同的code与Matlab中许多文件(在同一目录下不同的文件名)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一千个.dat文件具有相同的程序运行。有没有更快的方法或脚本来运行它,而不是自动运行逐一? .dat文件有不同的文件名。

I have a thousand .dat files to run with the same program. Is there any faster way or script to run it automatically instead of run them one by one? The .dat files have different filenames.

该计划是这样的:

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

而ABD是文件名。我有成千上万不同的文件名的文件。它是由不断复制和粘贴这些文件名到程序并运行它有点讨厌。任何人都得到一个更快的方法或code这个?

The abd is the file name. I have thousands of files with different file names. It is a bit annoying by keep copying and pasting those filenames into the program and run it. Anyone got a faster way or code for this?

推荐答案

您可以使用目录中获得的文件列表,然后在这样的循环处理它们。

you can use "dir" to get a list of files, and then process them in a loop like this.

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

end

这篇关于如何运行相同的code与Matlab中许多文件(在同一目录下不同的文件名)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆