How to load the folders in alphabetic/numeric order?

6 vues (au cours des 30 derniers jours)
Changoleon
Changoleon le 17 Mai 2017
Commenté : Stephen23 le 18 Mai 2017
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 commentaires
Stephen23
Stephen23 le 17 Mai 2017
Modifié(e) : Stephen23 le 17 Mai 2017
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 le 17 Mai 2017
@Stephen: Now we are even ;-) I'm glad that I had suggested this excellent tool some minutes before.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 17 Mai 2017
Modifié(e) : Jan le 17 Mai 2017
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 commentaire
Changoleon
Changoleon le 18 Mai 2017
Thank you for your reply. I actually looked up on it but it seems that I have missed it. Thanks again.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 18 Mai 2017
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 commentaire
Stephen23
Stephen23 le 18 Mai 2017
Nice idea... until one day you add the 1000th image into that directory.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by