【问题标题】:Comparing the contents of two folders in MATLAB [duplicate]比较MATLAB中两个文件夹的内容[重复]
【发布时间】:2024-01-04 04:26:01
【问题描述】:

我写了一个函数compareTGZ 用于比较两个tgz 文件夹。 tgz 文件夹包含以下类型的文件: - mat 文件和文本文件,例如 .m.ddf.txt

函数定义如下:

function [testStatus, testMessage] = compareTGZ(refTGZFile, newTGZFile)

我想添加一个条件来检查 refTGZFile 中存在的文件,而不是 newTGZFile 中的文件,反之亦然。

if lenOffnames_old > lenOffnames_new || lenOffnames_old < lenOffnames_new
    for i=1:lenOffnames_old
    % Split the path of fnames_old with delimiter filesep
        refTGZParts = strsplit(fnames_old{i}, filesep);
        % Split the path of fnames_new with delimiter filesep
        newTGZParts = strsplit(fnames_new{i}, filesep);

        if(strcmp(refTGZParts{3},newTGZParts{3}))==0; 
            testStatus = 0;
            % Return files in Reference tgz which are not found in Test tgz
            fprintf('File %s in Reference tgz is not found in Test tgz\n',refTGZParts{3});
            % Return files in Test tgz which are not found in Reference tgz
            fprintf('File %s in Test tgz is not found in Reference tgz\n',newTGZParts{3});
        end
    end

结束

refTGZFile 包含的文件多于newTGZFile 时,我会得到正确的结果。但是newTGZFile 包含的文件比refTGZFile 多,我得到一个错误。

请有人建议我如何解决这个错误。

【问题讨论】:

  • 您的逻辑假设右侧的文件名始终与左侧的文件名在列表中的位置相同。正如您在结果中看到的那样,这是一个糟糕的假设。您需要在不假设任何顺序的情况下比较文件名。考虑使用intersectismember 之类的方法

标签: arrays matlab cell


【解决方案1】:

您应该能够使用setdiff 轻松确定其中存在的文件而不是另一个存在的文件。

%// Create a temporary directory to untar everything to
tmpdir = tempname;

%// Extract the reference .tgz to this location
fnames_old = untar(refTGZFile, tmpdir);

%// Delete the temporary directory
rmdir(tmpdir, 's')

%// Extract the other .tgz file to the same location
fnames_new = untar(newTGZFile, tmpdir);

%// Use setdiff to compare the files that were in one but not the other
in_old_but_not_new = setdiff(fnames_old, fnames_new);
in_new_but_not_old = setdiff(fnames_new, fnames_old);

%// Clean up the temporary folder
rmdir(tmpdir, 's')

如果您不想像这样将所有内容提取到新位置,并且您有两个绝对路径的列表,则可以将它们转换为相对路径,然后进行比较。

%// Anonymous function to create a relative path
relpath = @(base,pth)regexprep(pth, ['^', regexptranslate('escape', base)], '');

fnames_old = untar(refTGZFile, oldTGZ);
fnames_new = untar(newTGZFile, newTGZ);

%// Convert everything to relative paths
fnames_old_relative = relpath(oldTGZ, fnames_old);
fnames_new_relative = relpath(newTGZ, fnames_new);

%// Compare what is in one but not the other.
is_old_but_not_new = setdiff(fnames_old_relative, fnames_new_relative);
is_new_but_not_old = setdiff(fnames_new_relative, fnames_old_relative);

然后打印出结果

for k = 1:numel(is_old_but_not_new)
    fprintf('File %s in Reference tgz is not found in Test tgz\n', is_old_but_not_new{k});
end

for k = 1:numel(is_new_but_not_old)
    fprintf('File %s in Test tgz is not found in Reference tgz\n', is_new_but_not_old{k});
end

【讨论】:

  • @Maliva 查看更新。
  • tmpname 是什么?因为我的输入是 refTGZFile 和 newTGZFile
  • @Maliva 这是一个错字。应该是tempname。不过,您实际上想要答案的第二部分。
  • 是的,我的问题在于第二部分
  • @Maliva 第二部分没有tmpname变量