在Matlab中循环播放多个文件 [英] Looping over multiple files in Matlab

查看:267
本文介绍了在Matlab中循环播放多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让Matlab R2013a在目录中查找所有扩展名为'.txt'的文件,然后对这些文件进行某些数学表达式.然后,脚本必须将文件中的数据打印出来,该文件的名称与输入文件的名称相同,只是添加了一些新单词,以便让我分辨出区别,例如:

I need to get Matlab R2013a to look in a directory for all files containing the '.txt' extension and then do certain mathematical expressions on those files. Afterwards, the script must print out the data in a file that is labelled with the same name as the input file, except with a few new words added so that I can tell the difference, such as:

Input:
file1.txt
file2.txt
Output: 
processed_file1.txt
...etc

我试图让matlab加载目录列表并以这种方式通过操作循环文件,但是我只获得单个文件的输出,而不是文件夹中的数百个输出.感谢您的帮助.

I have tried getting matlab to load a directory list and loop the files through the operations that way but I am only getting output for a single file instead of the several hundred in the folder. Thanks for any help.

推荐答案

不太困难.只需创建一个已处理"文件夹并将其保存在此处即可.不要忘记您可以随意使用MATLAB中的内置外壳.以下代码中最重要的行是前两行,然后是filename行.他们创建新文件夹(第1行),将.txt内容读取到名为data的结构中(第2行),然后检索文件名.请注意,如果需要打开和保存文件到不同的循环中,可以轻松地将filename设置为数组.

Not too difficult. Just make a 'processed' folder and save them there. Don't forget the built in shell in MATLAB at your disposal. The most important lines in the following code are the first two and then the filename line. They make the new folder (line 1), read the .txt contents into a struct called data (line 2), then the file names are retrieved. Note that you can easily make filename an array if you need to open and save the files in different loops.

mkdir processed     
data = dir('*.txt');   

for i = 1:length(data)
    filename = data(i).name; 

    % read data and do your processing
    % then save with something like:

    fid = fopen(['processed\' filename],'w'); 
    fprintf(...)   
    fid = fclose(fid);
end

这篇关于在Matlab中循环播放多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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