ordering a listbox by extension

3 views (last 30 days)
Jason
Jason on 8 Dec 2014
Commented: Jason on 8 Dec 2014
Is it possible to load two types of files into a list box BUT order not by filename but with the same extensions.
I use this, but it appears to order by date. I would like to group the two sets of files together so If I am only loading *.jpg and *.xls, I need all the jpgs ordered first, and then all the xls.
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder1, name, extension] = fileparts(baseFileName);
extension = upper(extension);
ListOfImageNames = [ListOfImageNames baseFileName];
end
set(handles.listbox1, 'string', ListOfImageNames);

Answers (1)

Joseph Cheng
Joseph Cheng on 8 Dec 2014
two ways to accomplish this, stack the extensions as well and use sort/sortrows to order the extensions. with the sorting functions you can get the reordering index and apply that to your list of files.
second method would be how you're creating your list of files. are you using dir()? if so you can use the wildcard filter in it to compile 2 lists.
ImageFiles=dir('yourFolder\*.jpg'); %to get the list of jpegs
XLSfiles=dir('yourfolder\*.xls'); %to get the list of xls files
set(handles.listbox1,'string',[ImageFiles;XLSfiles]);
  1 Comment
Jason
Jason on 8 Dec 2014
Thanks for the suggestion. Its not always the same type of files, so although I like the 2nd idea of stacking the images, its not really practical. So is my only option to use sort and figure out how to sort with an extension? Thanks

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!