How to sort a structure array?

3 views (last 30 days)
Jyothi Alugolu
Jyothi Alugolu on 17 Apr 2017
Edited: Stephen on 26 Apr 2021
hello,i have a IITD database...i want to load and save all the images files by natural order i.e from 1_1,1_2...till end of the database can i use sort function for this? i tried using natsortfiles..but of no use.. code i tried:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, 'C:\Users\admin\Desktop\Stripoutput\strip');
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
listOfFolderNames = [listOfFolderNames singleSubFolder];
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
filePattern = sprintf('%s/*.bmp', thisFolder);
baseFileNames = dir(filePattern);
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
imageArray{f} = [imread(fullFileName)];
for threshold=0.1:0.05:0.5;
fprintf(' Processing image file %s\n', fullFileName);
fprintf(' Folder %s has no image files in it.\n', thisFolder);
basefilename contains 1058*1 struct....which of different order..i want all the images in sorted order..

Answers (1)

Stephen on 17 Apr 2017
Edited: Stephen on 26 Apr 2021
"i tried using natsortfiles..but of no use"
I am surprised that my FEX submission was no use to you, considering that it was written to sort filenames alphanumerically, exactly as you seem to wish to do. Not only that, it was extensively texted, and I have not had one single person report that it does not work as documented, so perhaps:
  1. you have discovered a bug that no one else found: please let me know exactly how to replicate this bug and I will fix it.
  2. you did not download it form FEX: please download it from the given link, unzip it, and put the files onto your MATLAB path.
  3. you want a different order (not a "natural order"): please give an example of the order that you require.
  4. you are using it incorrectly: please note that there are usage examples in the FEX description, usage examples in the HTML help, and usage examples in the Mfile help. Anyone with internet access can read all of those examples for free. Did you look at them? Did you try them?
In case it your favorite internet search engine is momentarily not working and you do not wish to try using another internet search engine to easily locate those examples yourself, I have copied one of them here:
P = 'absolute/relative path to where the files are svaed';
S = dir(fullfile(P,'*.txt')); % get list of files in directory
S = natsortfiles(S); % sort file names into order
for k = 1:numel(S)
F = fullfile(P,S(k).name)
Which is trivially adapted to your code like this:
baseFileNames = dir(filePattern);
baseFileNames = natsortfiles(baseFileNames);
imageArray = cell(size(baseFileNames));
for k = 1:numel(baseFileNames)
fullFileName = fullfile(thisFolder, baseFileNames(k).name);
fprintf(' Processing image file %s\n', fullFileName);
imageArray{f} = imread(fullFileName);
I also fixed several other "features" in your code, such as correctly preallocating the cell array before the loop, and removing the totally pointless if.


Community Treasure Hunt

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

Start Hunting!