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

6 次查看(过去 30 天)
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 个评论
SK
SK 2014-10-2
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.

请先登录,再进行评论。

采纳的回答

SK
SK 2014-10-2
编辑:SK 2014-10-2
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 个)

Image Analyst
Image Analyst 2014-10-8

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by