필터 지우기
필터 지우기

accessing same file names in two different directories for comparison

조회 수: 13 (최근 30일)
Alex
Alex 2017년 12월 7일
댓글: Jan 2017년 12월 10일
Please let me know if there is a better title or way to ask this question. I don't believe I know the proper jargon.
I have some code that opens 10 folders called Plane1-Plane10 within a current directory called 'allwork/FillF3'. Each Plane* contains a text file called Data.txt:
proot='Plane*';% FOLDER template
hroot='Data.txt'; % FILENAME template
% directory engine
d=dir(proot);
d={d([d.isdir]).name}.';
hnam=cellfun(@(x) sprintf('%s%s%s',x,filesep,hroot),d,'uni',false);
for i=1:m
hfile=hnam{i};
disp(sprintf('working on file %s',hfile));
hnam{i}=importdata(hfile); % code to read contents of CFILE
Now, what I want to do is use this code for two working directories 'allwork/FillF3' AND 'neutral/FillF3'. so I can plot and compare the data from hnam{i} agains one another. How can I modify my folder template to do so? Here is what I tried with 'allwork/FillF3' AND 'neutral/FillF3' in a current working directory called 'testdir':
proot='neutral/FillF3/Plane*';% FOLDER template
proot2='allwork/FillF3/Plane*';% FOLDER template
hroot='Data.txt'; % FILENAME template
% directory engine
d=dir(proot);
d2=dir(proot2);
d={d([d.isdir]).name}.';
d2={d2([d2.isdir]).name}.';
m=length(d);
m2=length(d);
hnam=cellfun(@(x) sprintf('%s%s%s',x,filesep,hroot),d,'uni',false);
hnam2=cellfun(@(x) sprintf('%s%s%s',x,filesep,hroot),d2,'uni',false);
for i=1:m
hfile=hnam{i};
hfile2=hnam2{i};
disp(sprintf('working on file %s',hfile));
disp(sprintf('working on file %s',hfile2));
hnam{i}=importdata(hfile); % code to read contents of CFILE
hnam2{i}=importdata(hfile2); % code to read contents of CFILE
end
Cheers,
Alex
  댓글 수: 4
Alex
Alex 2017년 12월 10일
I just ran it again and hfile is currently 'Plane2\Data.txt', but should be 'neutral\FillF3\Plane2\Data.txt' so I guess line
hnam=cellfun(@(x) sprintf('%s%s%s',x,filesep,hroot),d,'uni',false);
Greg
Greg 2017년 12월 10일
Ahh, R2016b introduced recursive dir. Would greatly simplify your code.

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

채택된 답변

Jan
Jan 2017년 12월 10일
The anonymous functions are more complicated than useful here. Try this:
Folder1 = 'neutral/FillF3/Plane*';% FOLDER template
Folder2 = 'allwork/FillF3/Plane*';% FOLDER template
FileName = 'Data.txt'; % FILENAME template
DirList1 = dir(Folder1);
DirList2 = dir(proot2);
SubFolderIndex1 = find([DirList1.isdir]);
SubFolderIndex2 = find([DirList2.isdir]);
nSubFolder = length(SubFolderIndex1);
for iSub = 1:numel(SubFolderIndex1)
sub1 = SubFolderIndex1(iSub);
File1 = fullfile(DirList1(iSub).folder, DirList1(iSub).name, FileName);
sub2 = SubFolderIndex2(iSub);
File2 = fullfile(DirList2(iSub).folder, DirList2(iSub).name, FileName);
...
Does this match your needs? fullfile performs the concatenation dynamically inside the loop.
  댓글 수: 3
Alex
Alex 2017년 12월 10일
Got it working though using your suggestion:
proot2='allwork/FillF3/Plane*';% FOLDER template
Folder1 = 'neutral/FillF3/Plane*';% FOLDER template
Folder2 = 'allwork/FillF3/Plane*';% FOLDER template
folder1 = 'neutral/FillF3/';% FOLDER template
folder2 = 'nallwork/FillF3/';% FOLDER template
FileName = 'Data.txt'; % FILENAME template
DirList1 = dir(Folder1 );
DirList2 = dir(proot2);
SubFolderIndex1 = find([DirList1.isdir ]);
SubFolderIndex2 = find([DirList2.isdir]);
nSubFolder = length(SubFolderIndex1 );
for iSub = 1:numel(SubFolderIndex1 )
sub1 = SubFolderIndex1(iSub );
File1 = fullfile(folder1, DirList1(iSub).name, FileName );
sub2 = SubFolderIndex2(iSub );
File2 = fullfile(folder2, DirList2(iSub).name, FileName );
hnam{iSub}=importdata(File1); % code to read contents of CFILE
end
Thank you for your help!
Jan
Jan 2017년 12월 10일
@Alex: I have overseen, that you use R2010A. The .folder fields is replied by dir() in modern Matlab versions only.

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

추가 답변 (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