循环.fig文件并根据文件名将它们分组到文件夹中

时间:2016-09-21 22:29:40

标签: matlab

我有很多以这样命名的.fig文件:tts.speak("I want to speak", 2, null, generateUtterance()); 20160922_01_id_32509055.fig等等。

所以我认为我创建了一个循环遍历文件夹中所有.fig文件的脚本,并根据文件名中的最后一个数字将它们分组(复制)到另一个文件夹中。该文件夹是根据ID号创建的。这可能吗?

我一直在寻找其他涉及循环文件夹的解决方案,但我很新鲜。当我学习在Matlab中做其他事情时,这将使我更容易检查.fig文件。

2 个答案:

答案 0 :(得分:2)

MATLAB可以实现一切!我们可以使用dir获取所有.fig文件,然后使用regexp获取每个文件名的数字部分,然后使用copyfile将文件复制到其新主页。如果您想要移动它,可以改用movefile

% Define where the files are now and where you want them.
srcdir = '/my/input/directory';
outdir = '/my/output/directory';

% Find all .fig files in the source directory
figfiles = dir(fullfile(srcdir, '*.fig'));
figfiles = {figfiles.name};

for k = 1:numel(figfiles)
    % Extract the last numeric part from the filename
    numpart = regexp(figfiles{k}, '(?<=id_)\d+', 'match', 'once');

    % Determine the folder we are going to put it in
    destination = fullfile(outdir, numpart);

    % Make sure the folder exists
    if ~exist(destination, 'dir')
        mkdir(destination)
    end

    % Copy the file there!
    copyfile(fullfile(srcdir, figfiles{k}), destination)
end

答案 1 :(得分:1)

以下是如何识别和复制文件的示例。我让你做for循环:)

>> Figs = dir('*.fig');    % I had two .fig files on my desktop
>> Basename = strsplit(Figs(1).name, '.'); 
>> Id = strsplit(Basename{1}, '_');
>> Id = Id{3};
>> mkdir(fullfile('./',Id));
>> copyfile(Figs(1).name, fullfile('./',Id));

使用命令来查看他们的操作。它应该是直截了当的:))

相关问题