【问题标题】:Renaming files in mass in MATLAB在 MATLAB 中批量重命名文件
【发布时间】:2015-09-17 13:07:48
【问题描述】:

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

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

【问题讨论】:

  • 你根本没有说你已经尝试过什么。如果您不了解 Matlab 是如何读取文件的,请尝试搜索他们的文档。
  • 我已经编辑了问题。

标签: matlab file concatenation renaming


【解决方案1】:

这样的事情应该可以帮助您入门:

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

【讨论】:

  • 我遇到的一个问题是它在 1_1 之后直接订购 1_10 之类的文件,或者在 1_2 之后订购 1_20 之类的文件。首选格式是在下划线后按递增顺序排列,例如 1_1、1_2...1_9、1_10、1_10....1_19、1_20、1_21
  • 我认为顺序不重要。您可以乱序循环,然后从旧文件名中读取数字并适当地重命名。请参阅我编辑的答案。
  • 感谢您的反馈,很抱歉打扰您,但我也注意到 %3s 术语实际上会取全名(包括目录)的前 3 个字符,例如“ c:\"
  • 我不太了解 Matlab,无法真正回答您的问题,但就个人而言,我会使用 Excel 和 VBA 来完成此类工作。请看这个。 -- jpsoftwaretech.com/renaming-files-in-a-folder-using-vba
猜你喜欢
  • 2013-12-19
  • 2013-12-16
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2012-09-02
  • 1970-01-01
相关资源
最近更新 更多