0

In one of my folders (say Folder01) there are files like "IGN_A.txt", "IGN_B.txt", "IGN_C.txt".........

In another folder (say Folder02) there are files like "sim_IGN_A_M01.txt", "sim_IGN_A_M02.txt", "sim_IGN_A_M03.txt" for the corresponding file "IGN_A.txt" in Folder01.

Similarly, "sim_IGN_B_M01.txt", "sim_IGN_B_M02.txt", "sim_IGN_B_M03.txt" for corresponding file "IGN_B.txt" in Folder01.

How can I get the corresponding files from those Folders. For example, I want to get "IGN_A.txt" along with "sim_IGN_A_M01.txt", "sim_IGN_A_M02.txt", "sim_IGN_A_M03.txt". Here. I added my code which can only get "IGN_A.txt" along with "sim_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
2
  • You need fileparts and fullfile, and also simple concatenation. Commented Mar 5, 2019 at 13:53
  • Can you make changes in my code? I tried it in different way but couldn't manage it. Commented Mar 5, 2019 at 13:55

1 Answer 1

1

If the naming convention is consistent as provided by you, this would be my suggestion:

% 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
Sign up to request clarification or add additional context in comments.

3 Comments

Use glob if you want a cell array of filenames, don't expect that file extensions are always 3 chars
@Andy You're right, thanks for your hint. I haven't paid attention to that, since I assumed, that there'll be only txt files.
btw, there is fileparts to split dir, filename and extension (I should have mentioned that before in my comment)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.