How to load the folders in alphabetic/numeric order?

7 ビュー (過去 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 Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by