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

조회 수: 2 (최근 30일)
YJ
YJ 2014년 10월 2일
답변: Image Analyst 2014년 10월 8일
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
YJ
YJ 2014년 10월 7일
thanks, I used genpath funtion as you recommended

댓글을 달려면 로그인하십시오.

추가 답변 (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