How to sort the struct array after reading number of images using dir command

1 回表示 (過去 30 日間)
Mohammad Bhat
Mohammad Bhat 2018 年 2 月 21 日
編集済み: Stephen23 2021 年 4 月 18 日
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 件のコメント
Stephen23
Stephen23 2018 年 2 月 21 日
Mohammad Bhat's "Answer" moved here:
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.
Stephen23
Stephen23 2018 年 2 月 21 日
編集済み: Stephen23 2018 年 2 月 21 日
@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?

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2018 年 2 月 21 日
編集済み: Stephen23 2021 年 4 月 18 日
"I am reading images thru 'dir' command from a folder but those are unsorted , I am expecting them as sorted...."
The order of the output of dir is determined by the OS and is not specified.
You can sort filenames very easily by using my FEX submission natsortfiles, which provides an alphanumeric sort for filenames (i.e. so that any numbers in the filenames are sorted into numeric order). You will need to download it, unzip it, and place the Mfiles on the MATLAB Search Path (e.g. in the current directory):
It is easy to use, and there are example in the Mfile and HTML documentation. Here is a basic working example:
D = 'C:\Test';
S = dir(fullfile(D,'*.txt'));
S = natsortfiles(S); % alphanumeric sort by filename
for k = 1:numel(N)
filename = fullfile(D,S(k).name)
...
end
  17 件のコメント
Mohammad Bhat
Mohammad Bhat 2018 年 2 月 22 日
If you provide me your e-mail , I can send my files to you...
Stephen23
Stephen23 2018 年 2 月 22 日
編集済み: Stephen23 2018 年 2 月 23 日
@Mohammad Bhat: please put both the code that you are running, natsortfiles and natsort files into a zip file and upload them into a new comment (not an answer).
Most importantly: please show the outputs to these commands:
which fileparts -all
which cellfun -all
Have you named any variable, function, or mfile fileparts ?

サインインしてコメントする。

その他の回答 (1 件)

Mohammad Bhat
Mohammad Bhat 2018 年 2 月 22 日
編集済み: Stephen23 2018 年 2 月 22 日
After following these steps, It worked for another function
https://in.mathworks.com/matlabcentral/fileexchange/10959-sort-nat--natural-order-sort
D = 'C:\Users\Hp\Desktop\3';
S = dir(fullfile(D,'*.jpg'));
C = {S.name};
[cs,index] = sort_nat(C);
cs
I am attaching '.mat' file after saving result
  10 件のコメント
Stephen23
Stephen23 2018 年 2 月 23 日
編集済み: Stephen23 2018 年 2 月 23 日
@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
Mohammad Bhat 2018 年 2 月 23 日
Bingo, yes I checked now, I was doing a mistake, you guessed it correctly, thanks a lot.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by