changing directory to do same work

조회 수: 11 (최근 30일)
YJ
YJ 2014년 10월 7일
답변: Image Analyst 2014년 10월 8일
I have a folder which has a name as 1250, 1270, 1290, 1310, 1330, 1370, 1390 (first level)
and inside that each folder it has folder name pos1, pos2, pos3... (second level)
some of them has pos5 and some only have up to pos4
Now for each pos# folder, I want to do exactly same work.
SO I want to compute the code,
Change folder name 1250\pos1
Do work
Change folder name 1250\pos2
Do work (same)
Change folder name 1250\pos3
Do work (same)
Change folder name 1270\pos1
Do work (same)
Change folder name 1270\pos2
Do work (same)
Change folder name 1290\pos1
Do work (same)
....
....
....
....
here is my code I have been working on.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1250:20:1390 % first level folder name
shot_path= 'D:\DME_Run\';
for i=1:5 %number of pos folder here just say 5 %second level
t= sprintf('%s%.4d\\pos%.1d\\',shot_path,k,i);
files = dir([fullfile(shot_path, y) '*.mat']);
load(files(1).name);
x = zeros(768,768);
%%%%%%%%%%%%%%%%%%%%work%%%%%%%%%%%%%%%%%%%
for j=1:500 %each folder has 500 image
load((['Process_shot_f',num2str(j),'.mat'])); % Load the files. Notice how "eval" is not needed.
x = x + Process_shot_fj; % x is the name of the variable.
end
Mean_process_image = x./ length(files); % Average.
%%%%%%%%%%%%%%%%%%%%work%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%save%%%%%%%%%%%%%%%%%%%
y= sprintf('%s','Mean_process_image', int2str(i) ,'.mat');
savedir = sprintf('D:\\data\\Process shot\\%.4d\\pos%d',k,i);
if i==1
mkdir(savedir);
end
save(fullfile(savedir, y), ['Mean_process_image_pos_i']);
end
end
%%%%%%%%%%%%%%%%%%%%save%%%%%%%%%%%%%%%%%%%
clear;
clc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

채택된 답변

Image Analyst
Image Analyst 2014년 10월 8일
See my demo to recurse into all subfolders. It uses genpath() to create a list of all folders. In the inside of the loop, you can do whatever operations on the file that you want such as loading them and creating "x".

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