【问题标题】:Matlab move multiple files in a directoryMatlab在一个目录中移动多个文件
【发布时间】:2022-01-15 07:07:50
【问题描述】:

使用 Matlab,我想根据名称将同一目录中存在的图像移动到两个新目录中。

目录中有两组image'名称:'neg-0.pgm', 'neg-1.pgm', 'neg-2.pgm', ... and 'pos-0.pgm' , 'pos-1.pgm', 'pos-2.pgm', ...

我尝试了不同的功能来更改图像目录,但我无法成功操作。

我的代码是:

if not(exist('./CarDataset/TrainImages/PosImages', 'dir'))
    mkdir ./CarDataset/TrainImages PosImages
end

if not(exist('./CarDataset/TrainImages/NegImages', 'dir'))
    mkdir ./CarDataset/TrainImages NegImages
end

trainIm = dir('./CarDataset/TrainImages/*.pgm');

for i = 1:size(trainIm, 1)
    if(strcmp(trainIm(i).name, 'neg*.pgm'))
        save(fullfile('./CarDataset/TrainImages/NegImages', ['neg-' num2str(i) '.pgm']))
    end
end

我没有收到任何错误,但新目录仍然是空的。

【问题讨论】:

  • if contains( trainIm(i).name, 'neg' ),其他的都是else
  • 有什么问题?你有错误吗?没有创建新目录?图片没有保存?您的代码的哪一部分有效,哪一部分无效?
  • @RobertoT 我没有收到任何错误,并且目录已正确创建,但图像未保存在其中。
  • 从未尝试使用save 来保存实际图像,仅使用矩阵或结构。也许您应该尝试其他方式而不是该功能。可能的方式:es.mathworks.com/matlabcentral/answers/23481-how-to-save-image & stackoverflow.com/questions/22344884/…

标签: matlab directory filenames subdirectory movefile


【解决方案1】:

我认为有两个问题:

1 - 在带有通配符 (*) 的 if 语句中使用 strcmp 可能无法正常工作

2 - 使用movefile 而不是savehttps://www.mathworks.com/help/matlab/ref/movefile.html

见下面的代码(创建新目录后使用):

    origDir = './CarDataset/TrainImages/';
    trainIm = dir([origDir '*.pgm']);
    
    for i = 1:size(trainIm, 1)
        origFile = trainIm(i).name;
        if contains(trainIm(i).name, 'neg'))
             newDir = './CarDataset/TrainImages/NegImages/';
        else
             newDir = './CarDataset/TrainImages/PosImages/';
        end
        movefile([origDir trainIm(i).name], newDir);
    end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多