how to sort push button
1 view (last 30 days)
Show older comments
i have 10 number of folders. when i run the main.m file the folders shows like 10 number of push buttons. the push button having the names of that folder. now i need to sort the push buttons by folder created date, please help me. thanks in advance
0 Comments
Accepted Answer
Jan
on 18 Jul 2013
Edited: Jan
on 18 Jul 2013
I assume, you want:
top_dir = dir(dir_name);
[dummy, index] = sort([top_dir.datenum]);
top_dir = top_dir(index);
Now the files and folders inside the struct top_dir are chronologically sorted.
Working with relative path names is dangerous. Matlab tries to be smart and searches in all folders of the Matlab path. Better use absolute paths and avoid to append a data folder to the Matlab path:
currentPath = cd;
dir_name = fullfile(currentPath, 'Code');
top_dir = dir(dir_name);
fullfile is smarter than hardcoding the file separator: 1. it considers existing separators, e.g. in "C:\", and it cares about the preferences of the operating system: \ or /
2 Comments
Jan
on 20 Jul 2013
@godwin: The code I have posted sorts the names of the folder according to the dates. Afterwards you create the buttons. And because the names are sorted already, the buttons have the same order also. So there is no reason to sort the buttons. It is not even meaningful to sort the buttons, when the contents of the buttons are sorted already.
Btw. char(Name1(1:end)) can be simplified to Name1.
More Answers (2)
Jan
on 17 Jul 2013
- Obtain the creation date of the folders by dir. The replied field datenum is the best value to sort for.
- Let sort find the wanted order.
- Set the strings according to this order.
If you need a more explicit implementation, show us, what you have done so far and ask more explicitly for a specific problem.
3 Comments
Jan
on 17 Jul 2013
Where did you get these names from? I guess you use the DIR command, and then you can use the .datenum field directly for sorting also. Please provide the relevant part of the code, because we cannot guess how you create this list of names.
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!