accessing same file names in two different directories for comparison

4 次查看(过去 30 天)
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);

请先登录,再进行评论。

采纳的回答

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