How to sort the struct array after reading number of images using dir command
Show older comments
Hi,
I am reading images thru 'dir' command from a folder but those are unsorted , I am expecting them as sorted....
file2=dir(file); // file contains path to the folder where I stored the images
for k = 1 : numel(file2)
im = imread(file2(k).name);
folder1=sprintf('%s%s%s%d',targetfolder,index,fi,j);
imwrite(im,[folder1,'.jpg']);
j=j+1;
end
5 Comments
Stephen23
on 21 Feb 2018
Sir, I tried it due to a usage of recursion in a function my system shows out of memory and halts despite having 8GB ram.
I have 106 images like attached image....my system gets stuck ...while using it

Stephen23
on 21 Feb 2018
@Mohammad Bhat: perhaps your code is recursive. But unless you show use that code we have no idea what you are doing. And if you don't show us any warning or error message then we have no idea why your code failed.
If you want help then you have to show us the code that you used, and the complete error message. This means all of the red text.
PS: Please do not use answers for comments. The comments are for commenting.
Stephen23
on 21 Feb 2018
source_file1= 'C:\Users\Hp\Desktop\New folder\';
targetfolder='C:\Users\Hp\Desktop\target\';
extn='*.jpg';
fg='\';
h=; % Total number of subfolders in main folder
% index tokeep track sub folders in main folder
index=num2str(h);
file = sprintf('%s%s%s%s',source_file1,index,fg,extn); % Whole images in current sub-folder
file1=sprintf('%s%s%s',source_file1,index,fg); % current Sub Folder tracking
file2=dir(file);
for k = 1 : numel(file2) % To keep track for all images in each sub-folder
im = imread(file2(k).name);
folder1=sprintf('%s%s%s%d',targetfolder,index,fi,j);
imwrite(im,[folder1,'.jpg']);
j=j+1;
end
Stephen23
on 21 Feb 2018
D='C:\Users\Hp\Desktop\3';
% where I have stored '.jpg' images 106 in number
S = dir(fullfile(D,'*.jpg'));
N = natsortfiles({S.name});
Error using natsortfiles (line 15)
endfirst input supplied is not a struct.
@Mohammad Bhat: that is an unusual error. Line 15 of natsortfiles (that I wrote) is a comment, with no executable code anywhere near it. Nowhere in any of the Mfiles of that FEX submission is there anything called endfirst. An internet search did not locate anything related to the term "endfirst".
Are you sure that you downloaded the function from the link that I gave you?
I wonder if this is an undocumented beta-feature: what MATLAB version are you using?
Accepted Answer
More Answers (1)
Mohammad Bhat
on 22 Feb 2018
Edited: Stephen23
on 22 Feb 2018
10 Comments
Mohammad Bhat
on 23 Feb 2018
Image Analyst
on 23 Feb 2018
I noticed you've accepted an answer. So is this question solved, or not? In your "Answer" here, I do not see any question or anything for us to do, just an announcment.
Mohammad Bhat
on 23 Feb 2018
Stephen23
on 23 Feb 2018
@Mohammad Bhat: if you are happy to help me I would like to find out why you are getting an infinite loop. This is the first time that anyone has reported this behavior using natsortfiles, and I would appreciate your help in discovering why this happens!
Mohammad Bhat
on 23 Feb 2018
Mohammad Bhat
on 23 Feb 2018
@Mohammad Bhat: can you please upload these three files in a new comment:
- natsortfiles.
- natsort.
- the file where natsortfiles is called.
It is interesting that the errors that you have shown show a different line each time. I checked the versions available online and neither of them make any kind of recursive calls.
Mohammad Bhat
on 23 Feb 2018
@Mohammad Bhat: thank you for uploading the files.
The problem is very simple: you put the wrong function into the file natsort.m. If you have a look at natsort you will see that you have actually copied the function natsortfiles into this file. This explains why you are getting recursive calls, because what you thought was natsort was just a copy of natsortfiles, and so you accidentally forced natsortfiles to call itself recursively.
I just checked the online versions and the functions are correctly named (matching the filenames), so you must have made this mistake when copying the functions somehow.
I recommend that you simply download the correct natsort.m and natsortfiles.m from the link in my answer, by clicking the big blue "Download" button at the top.
Thank you for your patience and assistance in providing the files and information that I requested. I am very glad to have figured out why it did not work!
Mohammad Bhat
on 23 Feb 2018
Categories
Find more on Entering Commands 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!