How to get corresponding file in another folder?

조회 수: 1 (최근 30일)
Mr. 206
Mr. 206 2018년 12월 6일
댓글: Mr. 206 2018년 12월 10일
In one of my folders there are files like "IGN_A.txt", "IGN_B.txt", "IGN_C.txt".........
In another folder there are corresponding files like "sim_IGN_A.txt", "sim_IGN_B.txt", "sim_IGN_C.txt".............
Each of this file contains two columns of same size. How can I create a matrix where two column data from "IGN_A.txt" and two column data from "sim_IGN_A.txt" will be stored side by side. Same thing will be applied to other files.

채택된 답변

Cris LaPierre
Cris LaPierre 2018년 12월 7일
You can include path information in your load command. If you know the path, use that along with the filename to open both files. From there, just load the data from both files, and then create the two matrices combining the data as you wish.
Look into fullfile to help you assemple the full path and filename.
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2018년 12월 7일
편집: Cris LaPierre 2018년 12월 7일
Allfiles_Exp will be all files in that folder that match the filter '*ign*.txt'.
Allfiles_Exp_Sim will be all files in that folder that match the filter '*sim_ign*.txt'. There is no connection between the files loaded in either location.
It sounds like instead, Allfiles_Exp_Sim should be the filenames in Allfiles_Exp prepended with 'sim_'. If your naming convention is strictly adhered to, then there is no need to use dir on the Sim folder. Instead, use fullfile with the path to the Sim folder plus the names you get from the Exp folder prepended with 'sim_' to load each file pair.
BTW, the second for loop in your code loads every Sim file for a single Exp file. It is unnecessary with this new approach and can be removed.
Flame_speed__Exp_folder = './ali/LFS/test/Test_rig';
Flame_speed__Sim_folder = './ali/PostProcess';
% To get all the files in that directory and with desired file name pattern.
Allfiles_Exp = dir(fullfile(Flame_speed__Exp_folder, '*ign*.txt'));
for k = 1:length(Allfiles_Exp)
fullFileName = fullfile(Allfiles_Exp(k).folder, Allfiles_Exp(k).name));
READ=dlmread(fullFileName,'',1,0,"emptyvalue",911911911);
fullFileName_Sim = fullfile(Flame_speed__Sim_folder, strcat('sim_',Allfiles_Exp(k).name);
READ=dlmread(fullFileName_Sim,'',1,0,"emptyvalue",911911911);
...
end
Incidentally, your inputs for dlmread are giving me an error. If it is working for you, then disregard. However, the 'emptyvalue' argument is not an option per the documentation.
Mr. 206
Mr. 206 2018년 12월 10일
Thank you so much.
I was doing so because in future there can be files like "sim_IGN_A_Mech01.txt", "sim_IGN_A_Mech02.txt", "sim_IGN_A_Mech03.txt"........... which all correspond to same "IGN_A.txt" file.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by