Code covered by the BSD License  

Highlights from
File Ordering Patch Utility for MATLAB

3.5

3.5 | 2 ratings Rate this file 17 Downloads (last 30 days) File Size: 423 KB File ID: #29033
image thumbnail

File Ordering Patch Utility for MATLAB

by

 

15 Oct 2010 (Updated )

The utility patches the reordering considering the length of the filename during the sorting.

| Watch this File

File Information
Description

main executing reference usage: usage_DisplaySelectedAllImagesOfaFileTypeFromDirectory.m

Matlab file listing list the selected files in such order, which may cause some problems for certain bulk processing for benchmark applications.

imageFiles =

chp_mri_squareSized_0.jpg
chp_mri_squareSized_1.jpg
chp_mri_squareSized_10.jpg
chp_mri_squareSized_11.jpg
chp_mri_squareSized_12.jpg
chp_mri_squareSized_13.jpg
chp_mri_squareSized_14.jpg
chp_mri_squareSized_15.jpg
chp_mri_squareSized_16.jpg
chp_mri_squareSized_17.jpg
chp_mri_squareSized_18.jpg
chp_mri_squareSized_19.jpg
chp_mri_squareSized_2.jpg
chp_mri_squareSized_20.jpg
chp_mri_squareSized_21.jpg
chp_mri_squareSized_22.jpg
chp_mri_squareSized_23.jpg
chp_mri_squareSized_24.jpg
chp_mri_squareSized_25.jpg
chp_mri_squareSized_26.jpg
chp_mri_squareSized_27.jpg
chp_mri_squareSized_28.jpg
chp_mri_squareSized_29.jpg
chp_mri_squareSized_3.jpg
chp_mri_squareSized_4.jpg
chp_mri_squareSized_5.jpg
chp_mri_squareSized_6.jpg
chp_mri_squareSized_7.jpg
chp_mri_squareSized_8.jpg
chp_mri_squareSized_9.jpg

The utility patches the reordering in such manner that the files may be picked up in the following order.

imageFiles =

    'chp_mri_squareSized_0.jpg'
    'chp_mri_squareSized_1.jpg'
    'chp_mri_squareSized_2.jpg'
    'chp_mri_squareSized_3.jpg'
    'chp_mri_squareSized_4.jpg'
    'chp_mri_squareSized_5.jpg'
    'chp_mri_squareSized_6.jpg'
    'chp_mri_squareSized_7.jpg'
    'chp_mri_squareSized_8.jpg'
    'chp_mri_squareSized_9.jpg'
    'chp_mri_squareSized_10.jpg'
    'chp_mri_squareSized_11.jpg'
    'chp_mri_squareSized_12.jpg'
    'chp_mri_squareSized_13.jpg'
    'chp_mri_squareSized_14.jpg'
    'chp_mri_squareSized_15.jpg'
    'chp_mri_squareSized_16.jpg'
    'chp_mri_squareSized_17.jpg'
    'chp_mri_squareSized_18.jpg'
    'chp_mri_squareSized_19.jpg'
    'chp_mri_squareSized_20.jpg'
    'chp_mri_squareSized_21.jpg'
    'chp_mri_squareSized_22.jpg'
    'chp_mri_squareSized_23.jpg'
    'chp_mri_squareSized_24.jpg'
    'chp_mri_squareSized_25.jpg'
    'chp_mri_squareSized_26.jpg'
    'chp_mri_squareSized_27.jpg'
    'chp_mri_squareSized_28.jpg'
    'chp_mri_squareSized_29.jpg'

If the utility have more optimal approach, please do not hesitate to suggest and send feedback to author.
Email: promethevx@yahoo.com.

Thank you.

Regards,
Michael Chan JT

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
21 May 2014 Stephen Cobeldick

This submission is a very neat idea (thus the two stars), but it is let down by the code (thus the two stars). In particular, this submission:
- changes the input data multiple times between char and cell of strings arrays.
- uses unnecessary "for" loops… and changes the data type inside the loops (just use indexing!).
- concatenates the strings one-by-one to generate the output... with no array pre-allocation.
- has multiple "mlint" messages that need attention.
- include many lines of commented-out code.
- has misleading variable names: "sortedIndex" actually contains the string data itself.
- no help section, no information on the input or output specifications.
- unnecessary "sort": "unique" returns sorted data anyway.
- changes the input data type from cell of strings to output a char array.
- uses multiple files… for such a simple concept.

In fact the algorithm concept is simple enough, and here it is implemented in four lines of code plus input checking, with no loops and it keeps the input array orientation at the output:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = resortFileOrder5(inp)
% Simplified "resortFileOrder.m" that implements exactly the same sort concept without loops, and just five lines of code.
assert(iscellstr(inp),'Input must be a cell array of strings.')
tmp = sort(inp(:));
loes = cellfun('length',tmp);
swltc = arrayfun(@(n)tmp(n==loes),unique(loes),'UniformOutput',false);
out = reshape(vertcat(swltc{:}),size(inp));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

And as I noted below, by using a different concept this submission could be simplified into two lines of code plus input checking (and does not create multiple internal variables):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = resortFileOrder2(inp)
% Simplified "resortFileOrder.m" in just two lines of code.
assert(iscellstr(inp),'Input must be a cell array of strings.')
[~,idx] = sortrows([cellstr(char(32+cellfun('length',inp))),inp]);
out = reshape(inp(idx),size(inp));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

It is worth noting that this submission does NOT provide a natural order sort. If your data has leading zeros, if the numbers are located in different positions in the strings or has multiple numeric values inside the strings then this concept might not work:
temp = {'test2part2', 'test10part2','test1part10','test1part9'};
resortFileOrder(temp)
ans = […
'test1part9 ';
'test2part2 ';
'test10part2';
'test1part10']

As was mentioned by Douglas Schwarz below, there are other submissions on FEX that provide natural order sorting, eg using the above test data:
sortnat(temp)
ans = {'test1part9','test1part10','test2part2','test10part2'}

You can find Douglas Schwarzes natural order sort here:
http://www.mathworks.com/matlabcentral/fileexchange/10959
and my own customizable natural order sort here:
http://www.mathworks.com/matlabcentral/fileexchange/34464

14 May 2014 Stephen Cobeldick

Note: my comment below assumes a cell array of strings.

14 May 2014 Stephen Cobeldick

% Random arrangement of filenames for testing:
imageFiles = imageFiles(randperm(numel(imageFiles))).';
% Sort by length and name:
[~,idx] = sortrows([cellstr(char(32+cellfun('length',imageFiles))),imageFiles]);
imageFiles = imageFiles(idx)

Two lines of code replicates exactly the sorting behavior (on the example filenames). It would be nice to have a description of the other features of this submission, which actually seem quite interesting.

16 Oct 2010 Joan

very clean codes

15 Oct 2010 Michael Chan

Pardon the overlapping of the titles on the image display. The focus is on the patch. There is also a uility to display all images under a specified file type. It would also be good if we have an option for display all image types.

Thank you, Douglas Schwarz. I didn't find that initially. I have also always ensure that the coding is intuitive and commented.

15 Oct 2010 Jan Simon

I think the screen shot is funny, but rather confusing.

15 Oct 2010 Douglas Schwarz

See <http://www.mathworks.com/matlabcentral/fileexchange/10959-sortnat-natural-order-sort> for a similar function.

Updates
18 Oct 2010

>> Suggestion from Donn Shull
listFilesInDirectory.m:
Emend to avoid loops and may be quicker for a large number of files.

Contact us