If I have some file folders, like '19800101', '1980012',... '19801201',... In each file, it have a .mat file. I want to deal with folder according to if their belong to same month. like, .mat file in folder '19800101' and '19800102' should be done with same action.

댓글 수: 2

per isakson
per isakson 2016년 3월 3일
"In each file" &nbsp should that be "In each folder"?
Jason
Jason 2016년 3월 3일
Yes. In each folder. Thanks for correction

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

 채택된 답변

Ahmet Cecen
Ahmet Cecen 2016년 3월 3일

0 개 추천

Grab a list of all the folders in a given month using something like:
files = dir('198001*');
Check MATLAB Regex for more elaborate sub-set extraction schemes you might need later here: http://www.mathworks.com/help/matlab/ref/regexp.html
You can then read all mat files in those folders with a for loop like:
for k = 1:length(files)
Data{k} = load([files(k).name,'\nameofmatfileinside.mat']);
end
This assumes the name of the matfile is the same inside all of those folders, which I suspect is the case. Also the direction of the directory slash might differ based on operating system.

댓글 수: 7

Jason
Jason 2016년 3월 3일
Thanks for reply. What about the name of matfile is different, its name is the folder name, like '19800101.mat'?
I write by myself. I think it is works.
Data{k}=load([files(k).name, '\', files(k).name, '.mat'])
Jason
Jason 2016년 3월 3일
for "files = dir('198001*');" it seems to me I only can create it 12 times since there are 12 months? do you have more efficient way?
Something along the lines of the below should work:
for i=1:12
if i<10
currentrgx = ['19800',num2str(i),'*'];
else
currentrgx = ['1980',num2str(i),'*'];
end
files = dir(currentrgx);
end
No need for an if-statement:
for i=1:12,
currentgrx = sprintf('1980%02d*',i)
% files = dir(currentrgx);
end
Ahmet Cecen
Ahmet Cecen 2016년 3월 3일
Ooh, nice.
Jason
Jason 2016년 3월 4일
Thanks for providing such efficient algorithm

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

추가 답변 (0개)

카테고리

태그

질문:

2016년 3월 3일

댓글:

2016년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by