How to load the folders in alphabetic/numeric order?

11 次查看(过去 30 天)
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, ....

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by