在 MATLAB 中批量重命名文件

Renaming files in mass in MATLAB

在收集数据时,我目前使用以下格式命名我的文件:

1_10.mat

其中下划线前的数字是洲:

1- Africa
2- South America
3- Central America

第二个数字是测量所在大陆的日期。我想做的是将进行测量的国家也添加到文件名的末尾。例如:

1_1 --> 1_10 我想将每一个重命名为 1_1_Zaire --> 1_10_Zaire

1_11 --> 1_14,我想把每一个重命名为1_11_Kenya --> 1_11_Kenya

如何将所有 .mat 文件保存在同一个文件夹中?如果可能的话,我更愿意使用 MATLAB 进行重命名。

我理解算法将如下所示:

  1. 用所有 .mat 文件命名一个目录
  2. 创建一个从边界 1 到边界 x 的 for 循环
  3. 连接我想要的短语

唯一的问题是,我不知道如何获取循环的长度,也不明白MATLAB如何读取目录中的文件。

这是我试过的。

directory = 'C:\place';
for 1 : 9
    curName = directory.name;
    s = '_Africa';
    laterName = (strcat(directory,s)).name;
end

像这样的事情应该让你开始:

directory = 'C:\place\';

% Filter the list of files using * as a wildcard.
file = strcat(directory, '*.mat');

% Get a list of files and concatenate them with the directory name.
results = dir(file);

file_name = strcat(directory, '\', num2cell(char(results.name), 2)')';

% The total number of files
nfile = length(file_name)

% Loop through each file.
for i = 1: nfile
    curName = file_name{i}
    d = textscan(curName, '%3s%f%1s%f');
    if (d{2} == 1)
        if (1 <= d{4} && d{4} <= 10)
            laterName = sprintf('.\%i_%i_Zaire.mat', d{2}, d{4})
        elseif  (11 <= d{4} && d{4} <= 14)
            laterName = sprintf('.\%i_%i_Kenya.mat', d{2}, d{4})
        end
    else
        % ...
    end
end