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

6 visualizaciones (últimos 30 días)
YJ
YJ el 2 de Oct. de 2014
Respondida: Image Analyst el 8 de Oct. de 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 comentario
SK
SK el 2 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

SK
SK el 2 de Oct. de 2014
Editada: SK el 2 de Oct. de 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 comentario
YJ
YJ el 7 de Oct. de 2014
thanks, I used genpath funtion as you recommended

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 8 de Oct. de 2014

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by