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

2 Ansichten (letzte 30 Tage)
YJ
YJ am 2 Okt. 2014
Beantwortet: Image Analyst am 8 Okt. 2014
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 Kommentar
SK
SK am 2 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

SK
SK am 2 Okt. 2014
Bearbeitet: SK am 2 Okt. 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.
  1 Kommentar
YJ
YJ am 7 Okt. 2014
thanks, I used genpath funtion as you recommended

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 8 Okt. 2014

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by