How to load the folders in alphabetic/numeric order?

조회 수: 6 (최근 30일)
Changoleon
Changoleon 2017년 5월 17일
댓글: Stephen23 2017년 5월 18일
Hi all,
I have a folder in my desktop called RF. Inside RF, I have three folders that are called Day9, Day10, Day11. Each of these folders (day9...Day11) have an excel file inside. Now I want to write a code that goes inside Day 9, copies the numbers from excel file, then goes to Day 10, do the same thing, and go to day11 and do the same thing.
Here is what I have done so far:
clear all;
dirlist = uigetdir([],'Select highest folder with data');
cd(dirlist)
date_folders = dir(dirlist);
date_folders(~[date_folders.isdir]) = [];
rd = ismember( {date_folders.name}, {'.', '..'});
date_folders(rd) = [];
for d=1:length(date_folders)
disp(['running folder ' int2str(d) ' of ' int2str(length(date_folders))])
cd (date_folders(d).name)
num = xlsread('optical_properties.xlsx',1);
tumor(d,:) = num(1,:);
skin(d,:) = num (2,:);
cd ..
end
xlswrite('varname.xlsx',[tumor;skin]);
Now my code does the job, however there is a small mistake, it goes inside the folder RF, and then it opens Day 10 first instead of opening day9. It should keep the order (90,10,11) but it goes to 10,11, and then 9. Now according to my code,I need to somehow modify the date_folders which is a struct. How do I do that?
Any idea will help, Thanks in advance
Sina
  댓글 수: 2
Stephen23
Stephen23 2017년 5월 17일
편집: Stephen23 2017년 5월 17일
Download my FEX submission, I wrote it to sort filenames into numeric order:
There are plenty of examples in the Mfile and in the HTML help.
Jan
Jan 2017년 5월 17일
@Stephen: Now we are even ;-) I'm glad that I had suggested this excellent tool some minutes before.

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

채택된 답변

Jan
Jan 2017년 5월 17일
편집: Jan 2017년 5월 17일
Either create the names manually:
for index = 9:11
FolderName = sprintf('Day%d', index);
...
Or sort the names manually):
Folder = uigetdir([],'Select highest folder with data');
date_folders = dir(Folder);
date_namesS = sprintf('%s*', date_folders.name);
date_namesD = sscanf(date_nameS, 'Day%d*', Inf);
[dum, order] = sort(date_namesD);
folder_name = {date_folders(oder).name};
As you can imagine, other users had this problem before. And in such cases searching in the net, especially in the FileExchange will be useful. See:
Note: Using cd is prone to bugs. If a GUI or TIMER callback changes the current folder unexpectedly, the code fails. Maybe data are destroyed. Better use absolute file names, which contain the folder name:
num = xlsread(fullfile(date_folders(d).name, 'optical_properties.xlsx'),1)
  댓글 수: 1
Changoleon
Changoleon 2017년 5월 18일
Thank you for your reply. I actually looked up on it but it seems that I have missed it. Thanks again.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2017년 5월 18일
Another potential approach is to format your dates with a consistent number of digits. If you know or expect you are going to have more than 9 but fewer than 100 days, make your filenames Day01, Day02, ... Day09, Day10, .... If you know or expect you have more than 99 but fewer than 1000, make your filenames Day001, ... Day100, Day101, ....
  댓글 수: 1
Stephen23
Stephen23 2017년 5월 18일
Nice idea... until one day you add the 1000th image into that directory.

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

카테고리

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