How to load the folders in alphabetic/numeric order?
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Jan
il 17 Mag 2017
@Stephen: Now we are even ;-) I'm glad that I had suggested this excellent tool some minutes before.
Risposta accettata
Jan
il 17 Mag 2017
Modificato: Jan
il 17 Mag 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:
- https://www.mathworks.com/matlabcentral/fileexchange/10959-sort-nat--natural-order-sort
- https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
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)
Più risposte (1)
Steven Lord
il 18 Mag 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 Commento
Vedere anche
Categorie
Scopri di più su Environment and Settings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!