简体   繁体   English

八度:从特定目录加载所有文件

[英]Octave: Load all files from specific directory

I used to have Matlab and loaded all txt-files from directory "C:\\folder\\" into Matlab with the following code: 我曾经拥有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

If C:\\folder\\ contains A.txt, B.txt, C.txt, I would then have matrices A, B and C in the workspace. 如果C:\\ folder \\包含A.txt,B.txt,C.txt,那么我在工作区中将具有矩阵A,B和C。

The code doesn't work in octave, maybe because of "fullfile"?. 该代码不能以八度运行,可能是因为“ fullfile”? Anyway, with the following code I get matrices with the names C__folder_A, C__folder_B, C__folder_C. 无论如何,通过以下代码,我得到名称为C__folder_A,C__folder_B,C__folder_C的矩阵。 However, I need matrices called A, B, C. 但是,我需要称为A,B,C的矩阵。

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

Can you help me? 你能帮助我吗? Thanks, Martin 谢谢马丁

PS: The loop starts with 3 because files(1).name = . PS:循环从3开始,因为files(1).name =。 and files(2).name = .. 和files(2).name = ..

EDIT: I have just found a solution. 编辑:我刚刚找到一个解决方案。 It's not elegant, but it works. 它不优雅,但可以。 I just add the path in which the files are with "addpath", then I don't have to give the full name of the directory in the loop. 我只需使用“ addpath”添加文件所在的路径,然后就不必在循环中提供目录的全名。

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

It's usually bad design if you load files to variables which name is generated dynamically and you should load them to a cell array instead but this should work: 它通常是不好的设计,如果你加载的名字是动态生成的文件变量,你应该把它们加载到一个单元阵列,而不是但这应该工作:

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

The function scanFiles searches file names with extensions in the current dirrectory ( initialPath ) and subdirectories recursively . scanFiles函数以递归方式在当前目录( initialPath )和子目录中搜索带有extensions文件名。 The parameter fileHandler is a function that you can use to process populated file structure (ie read text, load image, etc.) 参数fileHandler是可用于处理填充的文件结构(即,读取文本,加载图像等)的函数。

Source 资源

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

Usage 用法

# 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM