Matlab查找名称中包含文本的所有文件

Zan*_*nam 2 matlab

我的目录中有两种命名文件模式.

模式1:

'XXXXXX FixedCost_zz123.mat'
Run Code Online (Sandbox Code Playgroud)

模式2

'XXXXXX FixedVolume.mat'
Run Code Online (Sandbox Code Playgroud)

所以,基于上面我的文件夹中的一些真实文件的例子是:

'Sap FixedCost_pkz123.mat'
'ASDFG FixedCost_z1.mat'
'TUP112RA FixedCost_h1453.mat'
'as FixedVolume.mat'
'P1234L FixedVolume.mat'
'afg FixedVolume.mat'
Run Code Online (Sandbox Code Playgroud)

我希望能够根据我在任何实例中需要的内容找到'FixedCost'和'FixedVolume'类型的所有文件.我该如何实现这一目标?我对函数的输入可能是'FixedCost'或'FixedVolume'.

seb*_*ian 5

您可以使用dir带有指定文件名模式的参数的函数:

fixedCostFiles = dir('*FixedCost*.mat');
fixedVolumeFiles = dir('*FixedVolume.mat');
Run Code Online (Sandbox Code Playgroud)

如果你想要更复杂的选择并且不怕java,你也可以使用apache FileUtils,它们是MATLAB的java的一部分:

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html

具体来说,检查listFiles功能.