如何在Octave / MATLAB的另一个文件夹中获取相应的文件?

时间:2019-03-05 12:34:35

标签: octave

在我的其中一个文件夹(例如Folder01)中,有一些文件,例如“ IGN_A.txt”,“ IGN_B.txt”,“ IGN_C.txt” .........

在另一个文件夹(例如Folder02)中,文件夹01中的相应文件“ IGN_A.txt”中有诸如“ sim_IGN_A_M01.txt”,“ sim_IGN_A_M02.txt”,“ sim_IGN_A_M03.txt”之类的文件。

类似地,Folder01中对应文件“ IGN_B.txt”的“ sim_IGN_B_M01.txt”,“ sim_IGN_B_M02.txt”,“ sim_IGN_B_M03.txt”。

如何从那些文件夹中获取相应的文件。 例如,我想同时获得“ IGN_A.txt”,“ sim_IGN_A_M01.txt”,“ sim_IGN_A_M02.txt”,“ sim_IGN_A_M03.txt”。 这里。我添加了只能与“ sim_IGN_A.txt”一起获得“ IGN_A.txt”的代码。

Folder01 = 'Home/A1';
Folder02 = 'Home/A2';
%Going Throuh all the Folder01 files
Allfiles_Folder01 = dir(fullfile(Folder01, '*IGN*.txt'));

for k = 1:length(Allfiles_Folder01)
    fullFileName = fullfile(Folder01, Allfiles_Folder01(k).name);
    READ_Folder01=dlmread(fullFileName,'',2,0);


    fullFileName_Sim = fullfile(Folder02, strcat('sim_',Allfiles_Folder01(k).name))
    READ_Folder02=dlmread(fullFileName_Sim,'',1,0);
end

1 个答案:

答案 0 :(得分:1)

如果您提供的命名约定一致,这是我的建议:

% Get all filenames from Folder01 in cell array.
Allfiles_Folder01 = dir(fullfile(Folder01, '*IGN*.txt'));
Allfiles_Folder01 = {Allfiles_Folder01.name}

% Iterate all filenames from Folder01.
for k = 1:numel(Allfiles_Folder01)

  % Cut file extension from current filename.
  filename = Allfiles_Folder01{k};
  filename = filename(1:end-4);

  % Get all filenames from Folder02 with specific search string in cell array.
  Allfiles_Folder02 = dir(fullfile(Folder02, strcat('*', filename, '*.txt')));
  Allfiles_Folder02 = {Allfiles_Folder02.name}

  % Do stuff with filenames from Folder02 corresponding to filename from Folder01.
  % ...
  % ...

end