【问题标题】:Octave: Load all files from specific directoryOctave:从特定目录加载所有文件
【发布时间】:2023-03-31 08:21:01
【问题描述】:

我以前有 Matlab,并使用以下代码将目录“C:\folder\”中的所有 txt 文件加载到 Matlab 中:

myFolder = 'C:\folder\';
filepattern = fullfile(myFolder, '*.txt');
files = dir(filepattern);
for i=1:length(files)
eval(['load ' myFolder,files(i).name ' -ascii']);
end

如果 C:\folder\ 包含 A.txt、B.txt、C.txt,那么我将在工作区中有矩阵 A、B 和 C。

代码在 octave 中不起作用,可能是因为“fullfile”?无论如何,通过以下代码,我得到名称为 C__folder_A、C__folder_B、C__folder_C 的矩阵。但是,我需要称为 A、B、C 的矩阵。

myFolder = 'C:\folder\';
files = dir(myFolder);
for i=3:length(files)
eval(['load ' myFolder,files(i).name ' -ascii']);
end

你能帮帮我吗? 谢谢, 马丁

PS:循环从 3 开始,因为 files(1).name = .和文件(2).name = ..

编辑: 我刚刚找到了解决方案。它并不优雅,但它确实有效。 我只是用“addpath”添加文件所在的路径,然后我不必在循环中给出目录的全名。

myFolder = 'C:\folder\';
addpath(myFolder)
files = dir(myFolder);
for i=3:length(files)
eval(['load ' files(i).name ' -ascii']);
end

【问题讨论】:

    标签: matlab matrix load octave dir


    【解决方案1】:

    如果您将文件加载到名称是动态生成的变量中,并且您应该将它们加载到元胞数组中,这通常是不好的设计,但这应该可以:

    files = glob('C:\folder\*.txt')
    for i=1:numel(files)
      [~, name] = fileparts (files{i});
      eval(sprintf('%s = load("%s", "-ascii");', name, files{i}));
    endfor
    

    【讨论】:

      【解决方案2】:

      函数scanFiles 在当前目录(initialPath) 和子目录中递归地 搜索带有extensions 的文件名。参数fileHandler 是一个函数,可用于处理填充的文件结构(即读取文本、加载图像等)

      来源

      function scanFiles(initialPath, extensions, fileHandler)
        persistent total = 0;
        persistent depth = 0; depth++;
        initialDir = dir(initialPath);
      
        printf('Scanning the directory %s ...\n', initialPath);
      
        for idx = 1 : length(initialDir) 
          curDir = initialDir(idx);
          curPath = strcat(curDir.folder, '\', curDir.name);
      
          if regexp(curDir.name, "(?!(\\.\\.?)).*") * curDir.isdir
            scanFiles(curPath, extensions, fileHandler);
          elseif regexp(curDir.name, cstrcat("\\.(?i:)(?:", extensions, ")$"))
            total++;
            file = struct("name",curDir.name,
                           "path",curPath,
                           "parent",regexp(curDir.folder,'[^\\\/]*$','match'),
                           "bytes",curDir.bytes);
            fileHandler(file);
          endif
        end
      
        if!(--depth)
          printf('Total number of files:%d\n', total);
          total=0;
        endif
      endfunction
      

      用法

      # txt
      # textFileHandlerFunc=@(file)fprintf('%s',fileread(file.path));
      # scanFiles("E:\\Examples\\project\\", "txt", textFileHandlerFunc);
      
      # images
      # imageFileHandlerFunc=@(file)imread(file.path);
      # scanFiles("E:\\Examples\\project\\datasets\\", "jpg|png", imageFileHandlerFunc);
      
      # list files
      fileHandlerFunc=@(file)fprintf('path=%s\nname=%s\nsize=%d bytes\nparent=%s\n\n',
                     file.path,file.name,file.bytes,file.parent);
      scanFiles("E:\\Examples\\project\\", "txt", fileHandlerFunc);
      

      【讨论】:

        猜你喜欢
        • 2010-10-17
        • 2020-07-17
        • 2014-11-08
        • 2014-11-18
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多