changing directory to do same work

14 views (last 30 days)
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;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Accepted Answer

Image Analyst
Image Analyst on 8 Oct 2014
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".

More Answers (0)

Categories

Find more on Search Path 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!