- 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.
- you did not download it form FEX: please download it from the given link, unzip it, and put the files onto your MATLAB path.
- you want a different order (not a "natural order"): please give an example of the order that you require.
- 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?
How to sort a structure array?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 image..how 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
return;
end
% 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)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
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;
m{f}=im2bw(imageArray{f},threshold);
end
end
fprintf(' Processing image file %s\n', fullFileName);
end
fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
basefilename contains 1058*1 struct....which of different order..i want all the images in sorted order..
0 Commenti
Risposte (1)
Stephen23
il 17 Apr 2017
Modificato: Stephen23
il 26 Apr 2021
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:
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)
end
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);
...
end
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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Filename Construction 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!