accessing same file names in two different directories for comparison

5 views (last 30 days)
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 Comments
Alex
Alex on 10 Dec 2017
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 on 10 Dec 2017
Ahh, R2016b introduced recursive dir. Would greatly simplify your code.

Sign in to comment.

Accepted Answer

Jan
Jan on 10 Dec 2017
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 Comments
Alex
Alex on 10 Dec 2017
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 on 10 Dec 2017
@Alex: I have overseen, that you use R2010A. The .folder fields is replied by dir() in modern Matlab versions only.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!