[HELP NEEDED] loop over folder? and save them?

6 views (last 30 days)
pro_path = 'C:\Users\user\Desktop\thesis\xd_0001\'
~~~~~~~
Process all data inside pro_path and compute one data set call xd_0001
~~~~~~~
savedir = 'C:\Users\user\Desktop\Matlab\
save(fullfile(savedir, 'xd_0001.mat'), 'xd_0001');
pro_path = 'C:\Users\user\Desktop\thesis\xd_0002\'
~~~~~~~
Process all data inside pro_path and compute one data set call xd_0002
~~~~~~~
savedir = 'C:\Users\user\Desktop\Matlab\
save(fullfile(savedir, 'xd_0002.mat'), 'xd_0002');
pro_path = 'C:\Users\user\Desktop\thesis\xd_0003\'
~~~~~~~
Process all data inside pro_path and compute one data set call xd_0003
~~~~~~~
savedir = 'C:\Users\user\Desktop\Matlab\
save(fullfile(savedir, 'xd_0003.mat'), 'xd_0003');
So I have *100 of xd folders. and I am wondering is there easier way of doing it, instead having 100 of same codes over slight different folder name.
I do know how to do the loop over the files, but for directory I don't know how.... Also, I want to save them as a folder name
It did work with the following code, but there are 100 folders, and it seems it is bit stupid to do this way by putting all folder name into the folder_list...
folder_list = {'d:\folder1','d:\folder2','d:\folder3...................'};
for jj = 1 : length( folder_list )
pathname = folder_list{jj};
cd( pathname );
processs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1 Comment
SK
SK on 2 Oct 2014
Actually it is not so stupid, because if in future your folder names are not uniform, it will still work. You just need to find a way to generate the folder names, maybe using the genpath() function.

Sign in to comment.

Accepted Answer

SK
SK on 2 Oct 2014
Edited: SK on 2 Oct 2014
Assuming you want folder name, variable name and mat file name to be same:
prodir_0 = 'C:\Users\user\Desktop\thesis'
savedir = ''C:\Users\user\Desktop\Matlab';
num_folders = 100;
for i = 1 : num_folders
proname = ['xd_', sprintf('%04u', i)];
prodir = [prodir_0, filesep, proname];
% ... open and process directory prodir ...
% ... put result in temporary variable having any name (say Temp).
% Assuming your result is in a variable Temp
S.(proname) = Temp;
% This will save the struct field as a separate variable in the file
matfilename = [proname, '.mat'];
save([savedir, filesep, matfilename], '-struct', S);
S.(proname) = []; % clear struct field
end
Note: Code is untested - there may be typos etc.

More Answers (1)

Image Analyst
Image Analyst on 8 Oct 2014

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!