Apply code on matlab files in different directories ?
이전 댓글 표시
I want to apply the code on the .m files present in sub directories. The names of sub-directories started from 001 up to 365 in sequence. Could someone help me to write some code ? I have a very basic knowledge of Matlab scripts.
Thanks in advance!
답변 (2개)
David Sanchez
2015년 8월 18일
From Documentation:
path(path,'newpath') adds the newpath folder to the end of the search path. If newpath is already on the search path, then path(path,'newpath') moves newpath to the end of the search path.
In your case, you should do something like this:
your_path = path where you have your sub-directories;
for k=1:365 % sub-directories started from 001 up to 365 in sequence
str = strcat(your_path,'\000); % add the temporary 000 ending
file_number = num2str(k); % convert index to string
L = length(str); % length of your path
LK = length(file_number); % length of index as string
new_path = strcat(str(1:L-LK),file_number); % full new path od kth folder
path(path,new_path) % add the kth folder to matlab path
end
댓글 수: 11
geoabidali
2015년 8월 18일
geoabidali
2015년 8월 18일
Azzi Abdelmalek
2015년 8월 18일
geoabidali, to show your code, just copy and paste it.
Walter Roberson
2015년 8월 18일
your_path = path where you have your sub-directories;
for k = 1 : 365
new_path = fullfile(your_path, sprintf('%03d', k));
addpath(new_path);
end
geoabidali
2015년 8월 18일
편집: geoabidali
2015년 8월 18일
Walter Roberson
2015년 8월 18일
Why do you go through all that calculation trouble but not record the results for each directory, either by using a cell array or by writing it to a file?
For example
energy{K} = sum(psdest);
at the end of the loop would at least allow the calculated energy to be saved in a variable for use after the loop.
geoabidali
2015년 8월 18일
Walter Roberson
2015년 8월 18일
The problem is not in the calculation, the problem is that after you calculate the values you do not do anything with them, so the next file replaces the values.
Do you want values saved to a file? If so then which variables do you want saved, and do you want them saved to one file for every input file or do you want all the results combined into one output file?
geoabidali
2015년 8월 19일
Walter Roberson
2015년 8월 19일
You seem to be working with 365 different subdirectories, but I cannot tell which file or files you want to use out of each subdirectory. Is there a data_hr.mat file in each subdirectory that has a single variable in it? Is there a data_hr.csv file in each subdirectory? Is data_hr the name of a column in a .xls file that has a consistent name in each subdirectory? Is there a data_hr.m file in each subdirectory that is a function that returns data? Are there multiple files in each sub-directory that each need to be processed?
geoabidali
2015년 8월 19일
카테고리
도움말 센터 및 File Exchange에서 Search Path에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
