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 件のコメント
採用された回答
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 件のコメント
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 Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!