MATLAB-对目录中所有文件的迭代功能 [英] MATLAB - iterate function on all files in a directory

查看:55
本文介绍了MATLAB-对目录中所有文件的迭代功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个MATLAB函数,其功能类似于'myFunction('input','output')',其中输入和输出分别是输入和输出文件名.我需要在目录中的所有文件上实现此功能.是否可以遍历所有文件以在所有文件上实现代码?

I have written a MATLAB function, which works like 'myFunction('input', 'output')', in which input and output are the input and output file names respectively. I need to implement this function on all the files in a directory. Is it possible to loop through all files to implement the code on all files?

推荐答案

使用 dir 列出工作目录中的文件

use dir to list files in a working directory

%// list all text files in your folder
fls = dir( fullfile( 'path', 'to', 'my', 'folder', '*.txt' ) ); 
for ii = 1: numel(fls)
    infile = fullfile( 'path', 'to', 'my', 'folder', fls(ii).name );
    outfile = fullfile( 'path', 'to', 'my', 'folder', [fls(ii).name(1:end-4),'.DL'] ); 
    myFunction( infile, outfile );
end

请注意如何使用 fullfile 命令来连接路径和文件名,以对OS环境稳定的方式提供.

Note how fullfile command is used to concatenate paths and file name in a manner that is robust to OS environment.

此外,如 excaza 在他的

Moreover, as noted by excaza in his comment, it is best to use fileparts to separate the filename from its extension.

这篇关于MATLAB-对目录中所有文件的迭代功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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